Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 2 | /* |
| 3 | * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC |
| 4 | * Copyright (C) 2007 Hans Verkuil |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/videodev2.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 13 | #include <media/v4l2-device.h> |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 14 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 15 | |
| 16 | MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC"); |
| 17 | MODULE_AUTHOR("Hans Verkuil"); |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 20 | static bool debug; |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 21 | |
| 22 | module_param(debug, bool, 0644); |
| 23 | |
Niels de Vos | 61a2d07 | 2008-07-31 00:07:23 -0700 | [diff] [blame] | 24 | MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On"); |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 25 | |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 26 | struct cs5345_state { |
| 27 | struct v4l2_subdev sd; |
| 28 | struct v4l2_ctrl_handler hdl; |
| 29 | }; |
| 30 | |
| 31 | static inline struct cs5345_state *to_state(struct v4l2_subdev *sd) |
| 32 | { |
| 33 | return container_of(sd, struct cs5345_state, sd); |
| 34 | } |
| 35 | |
| 36 | static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) |
| 37 | { |
| 38 | return &container_of(ctrl->handler, struct cs5345_state, hdl)->sd; |
| 39 | } |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 40 | |
| 41 | /* ----------------------------------------------------------------------- */ |
| 42 | |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 43 | static inline int cs5345_write(struct v4l2_subdev *sd, u8 reg, u8 value) |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 44 | { |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 45 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 46 | |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 47 | return i2c_smbus_write_byte_data(client, reg, value); |
| 48 | } |
| 49 | |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 50 | static inline int cs5345_read(struct v4l2_subdev *sd, u8 reg) |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 51 | { |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 52 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 53 | |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 54 | return i2c_smbus_read_byte_data(client, reg); |
| 55 | } |
| 56 | |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 57 | static int cs5345_s_routing(struct v4l2_subdev *sd, |
| 58 | u32 input, u32 output, u32 config) |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 59 | { |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 60 | if ((input & 0xf) > 6) { |
| 61 | v4l2_err(sd, "Invalid input %d.\n", input); |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | } |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 64 | cs5345_write(sd, 0x09, input & 0xf); |
| 65 | cs5345_write(sd, 0x05, input & 0xf0); |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 69 | static int cs5345_s_ctrl(struct v4l2_ctrl *ctrl) |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 70 | { |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 71 | struct v4l2_subdev *sd = to_sd(ctrl); |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 72 | |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 73 | switch (ctrl->id) { |
| 74 | case V4L2_CID_AUDIO_MUTE: |
| 75 | cs5345_write(sd, 0x04, ctrl->val ? 0x80 : 0); |
| 76 | return 0; |
| 77 | case V4L2_CID_AUDIO_VOLUME: |
| 78 | cs5345_write(sd, 0x07, ((u8)ctrl->val) & 0x3f); |
| 79 | cs5345_write(sd, 0x08, ((u8)ctrl->val) & 0x3f); |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 80 | return 0; |
| 81 | } |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 82 | return -EINVAL; |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 86 | static int cs5345_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 87 | { |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 88 | reg->size = 1; |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 89 | reg->val = cs5345_read(sd, reg->reg & 0x1f); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Hans Verkuil | 977ba3b1 | 2013-03-24 08:28:46 -0300 | [diff] [blame] | 93 | static int cs5345_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 94 | { |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 95 | cs5345_write(sd, reg->reg & 0x1f, reg->val & 0xff); |
| 96 | return 0; |
| 97 | } |
| 98 | #endif |
| 99 | |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 100 | static int cs5345_log_status(struct v4l2_subdev *sd) |
| 101 | { |
| 102 | u8 v = cs5345_read(sd, 0x09) & 7; |
| 103 | u8 m = cs5345_read(sd, 0x04); |
| 104 | int vol = cs5345_read(sd, 0x08) & 0x3f; |
| 105 | |
| 106 | v4l2_info(sd, "Input: %d%s\n", v, |
| 107 | (m & 0x80) ? " (muted)" : ""); |
| 108 | if (vol >= 32) |
| 109 | vol = vol - 64; |
| 110 | v4l2_info(sd, "Volume: %d dB\n", vol); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 114 | /* ----------------------------------------------------------------------- */ |
| 115 | |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 116 | static const struct v4l2_ctrl_ops cs5345_ctrl_ops = { |
| 117 | .s_ctrl = cs5345_s_ctrl, |
| 118 | }; |
| 119 | |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 120 | static const struct v4l2_subdev_core_ops cs5345_core_ops = { |
| 121 | .log_status = cs5345_log_status, |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 122 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 123 | .g_register = cs5345_g_register, |
| 124 | .s_register = cs5345_s_register, |
| 125 | #endif |
| 126 | }; |
| 127 | |
| 128 | static const struct v4l2_subdev_audio_ops cs5345_audio_ops = { |
| 129 | .s_routing = cs5345_s_routing, |
| 130 | }; |
| 131 | |
| 132 | static const struct v4l2_subdev_ops cs5345_ops = { |
| 133 | .core = &cs5345_core_ops, |
| 134 | .audio = &cs5345_audio_ops, |
| 135 | }; |
| 136 | |
| 137 | /* ----------------------------------------------------------------------- */ |
| 138 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 139 | static int cs5345_probe(struct i2c_client *client, |
| 140 | const struct i2c_device_id *id) |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 141 | { |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 142 | struct cs5345_state *state; |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 143 | struct v4l2_subdev *sd; |
| 144 | |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 145 | /* Check if the adapter supports the needed features */ |
| 146 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 147 | return -EIO; |
| 148 | |
| 149 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 150 | client->addr << 1, client->adapter->name); |
| 151 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 152 | state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 153 | if (state == NULL) |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 154 | return -ENOMEM; |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 155 | sd = &state->sd; |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 156 | v4l2_i2c_subdev_init(sd, client, &cs5345_ops); |
| 157 | |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 158 | v4l2_ctrl_handler_init(&state->hdl, 2); |
| 159 | v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops, |
| 160 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0); |
| 161 | v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops, |
| 162 | V4L2_CID_AUDIO_VOLUME, -24, 24, 1, 0); |
| 163 | sd->ctrl_handler = &state->hdl; |
| 164 | if (state->hdl.error) { |
| 165 | int err = state->hdl.error; |
| 166 | |
| 167 | v4l2_ctrl_handler_free(&state->hdl); |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 168 | return err; |
| 169 | } |
| 170 | /* set volume/mute */ |
| 171 | v4l2_ctrl_handler_setup(&state->hdl); |
| 172 | |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 173 | cs5345_write(sd, 0x02, 0x00); |
| 174 | cs5345_write(sd, 0x04, 0x01); |
| 175 | cs5345_write(sd, 0x09, 0x01); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* ----------------------------------------------------------------------- */ |
| 180 | |
| 181 | static int cs5345_remove(struct i2c_client *client) |
| 182 | { |
| 183 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 184 | struct cs5345_state *state = to_state(sd); |
Hans Verkuil | b3ee7e2 | 2008-12-19 10:34:22 -0300 | [diff] [blame] | 185 | |
| 186 | v4l2_device_unregister_subdev(sd); |
Hans Verkuil | 34a078d | 2010-12-12 07:53:28 -0300 | [diff] [blame] | 187 | v4l2_ctrl_handler_free(&state->hdl); |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* ----------------------------------------------------------------------- */ |
| 192 | |
Jean Delvare | af29486 | 2008-05-18 20:49:40 +0200 | [diff] [blame] | 193 | static const struct i2c_device_id cs5345_id[] = { |
| 194 | { "cs5345", 0 }, |
| 195 | { } |
| 196 | }; |
| 197 | MODULE_DEVICE_TABLE(i2c, cs5345_id); |
| 198 | |
Hans Verkuil | 51e8684 | 2010-09-15 15:00:07 -0300 | [diff] [blame] | 199 | static struct i2c_driver cs5345_driver = { |
| 200 | .driver = { |
Hans Verkuil | 51e8684 | 2010-09-15 15:00:07 -0300 | [diff] [blame] | 201 | .name = "cs5345", |
| 202 | }, |
| 203 | .probe = cs5345_probe, |
| 204 | .remove = cs5345_remove, |
| 205 | .id_table = cs5345_id, |
Hans Verkuil | 6fb377f | 2007-12-18 19:40:44 -0300 | [diff] [blame] | 206 | }; |
Hans Verkuil | 51e8684 | 2010-09-15 15:00:07 -0300 | [diff] [blame] | 207 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 208 | module_i2c_driver(cs5345_driver); |