Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 2 | /* |
| 3 | * tlv320aic23b - driver version 0.0.1 |
| 4 | * |
| 5 | * Copyright (C) 2006 Scott Alfter <salfter@ssai.us> |
| 6 | * |
| 7 | * Based on wm8775 driver |
| 8 | * |
| 9 | * Copyright (C) 2004 Ulf Eklund <ivtv at eklund.to> |
| 10 | * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl> |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 16 | #include <linux/ioctl.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 18 | #include <linux/i2c.h> |
Hans Verkuil | 33b687c | 2008-07-25 05:32:50 -0300 | [diff] [blame] | 19 | #include <linux/videodev2.h> |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 20 | #include <media/v4l2-device.h> |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 21 | #include <media/v4l2-ctrls.h> |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 22 | |
| 23 | MODULE_DESCRIPTION("tlv320aic23b driver"); |
| 24 | MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 27 | |
| 28 | /* ----------------------------------------------------------------------- */ |
| 29 | |
| 30 | struct tlv320aic23b_state { |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 31 | struct v4l2_subdev sd; |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 32 | struct v4l2_ctrl_handler hdl; |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 33 | }; |
| 34 | |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 35 | static inline struct tlv320aic23b_state *to_state(struct v4l2_subdev *sd) |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 36 | { |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 37 | return container_of(sd, struct tlv320aic23b_state, sd); |
| 38 | } |
| 39 | |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 40 | static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) |
| 41 | { |
| 42 | return &container_of(ctrl->handler, struct tlv320aic23b_state, hdl)->sd; |
| 43 | } |
| 44 | |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 45 | static int tlv320aic23b_write(struct v4l2_subdev *sd, int reg, u16 val) |
| 46 | { |
| 47 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 48 | int i; |
| 49 | |
| 50 | if ((reg < 0 || reg > 9) && (reg != 15)) { |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 51 | v4l2_err(sd, "Invalid register R%d\n", reg); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 52 | return -1; |
| 53 | } |
| 54 | |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 55 | for (i = 0; i < 3; i++) |
| 56 | if (i2c_smbus_write_byte_data(client, |
| 57 | (reg << 1) | (val >> 8), val & 0xff) == 0) |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 58 | return 0; |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 59 | v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 60 | return -1; |
| 61 | } |
| 62 | |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 63 | static int tlv320aic23b_s_clock_freq(struct v4l2_subdev *sd, u32 freq) |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 64 | { |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 65 | switch (freq) { |
| 66 | case 32000: /* set sample rate to 32 kHz */ |
| 67 | tlv320aic23b_write(sd, 8, 0x018); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 68 | break; |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 69 | case 44100: /* set sample rate to 44.1 kHz */ |
| 70 | tlv320aic23b_write(sd, 8, 0x022); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 71 | break; |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 72 | case 48000: /* set sample rate to 48 kHz */ |
| 73 | tlv320aic23b_write(sd, 8, 0x000); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 74 | break; |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 75 | default: |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 81 | static int tlv320aic23b_s_ctrl(struct v4l2_ctrl *ctrl) |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 82 | { |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 83 | struct v4l2_subdev *sd = to_sd(ctrl); |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 84 | |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 85 | switch (ctrl->id) { |
| 86 | case V4L2_CID_AUDIO_MUTE: |
| 87 | tlv320aic23b_write(sd, 0, 0x180); /* mute both channels */ |
| 88 | /* set gain on both channels to +3.0 dB */ |
| 89 | if (!ctrl->val) |
| 90 | tlv320aic23b_write(sd, 0, 0x119); |
| 91 | return 0; |
| 92 | } |
| 93 | return -EINVAL; |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int tlv320aic23b_log_status(struct v4l2_subdev *sd) |
| 97 | { |
| 98 | struct tlv320aic23b_state *state = to_state(sd); |
| 99 | |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 100 | v4l2_ctrl_handler_log_status(&state->hdl, sd->name); |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 104 | /* ----------------------------------------------------------------------- */ |
| 105 | |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 106 | static const struct v4l2_ctrl_ops tlv320aic23b_ctrl_ops = { |
| 107 | .s_ctrl = tlv320aic23b_s_ctrl, |
| 108 | }; |
| 109 | |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 110 | static const struct v4l2_subdev_core_ops tlv320aic23b_core_ops = { |
| 111 | .log_status = tlv320aic23b_log_status, |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static const struct v4l2_subdev_audio_ops tlv320aic23b_audio_ops = { |
| 115 | .s_clock_freq = tlv320aic23b_s_clock_freq, |
| 116 | }; |
| 117 | |
| 118 | static const struct v4l2_subdev_ops tlv320aic23b_ops = { |
| 119 | .core = &tlv320aic23b_core_ops, |
| 120 | .audio = &tlv320aic23b_audio_ops, |
| 121 | }; |
| 122 | |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 123 | /* ----------------------------------------------------------------------- */ |
| 124 | |
| 125 | /* i2c implementation */ |
| 126 | |
| 127 | /* |
| 128 | * Generic i2c probe |
| 129 | * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' |
| 130 | */ |
| 131 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 132 | static int tlv320aic23b_probe(struct i2c_client *client, |
| 133 | const struct i2c_device_id *id) |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 134 | { |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 135 | struct tlv320aic23b_state *state; |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 136 | struct v4l2_subdev *sd; |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 137 | |
| 138 | /* Check if the adapter supports the needed features */ |
Hans Verkuil | 6235168 | 2007-09-14 05:03:17 -0300 | [diff] [blame] | 139 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Hans Verkuil | 188f345 | 2007-09-16 10:47:15 -0300 | [diff] [blame] | 140 | return -EIO; |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 141 | |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 142 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 143 | client->addr << 1, client->adapter->name); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 144 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 145 | state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 146 | if (state == NULL) |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 147 | return -ENOMEM; |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 148 | sd = &state->sd; |
| 149 | v4l2_i2c_subdev_init(sd, client, &tlv320aic23b_ops); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 150 | |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 151 | /* Initialize tlv320aic23b */ |
| 152 | |
| 153 | /* RESET */ |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 154 | tlv320aic23b_write(sd, 15, 0x000); |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 155 | /* turn off DAC & mic input */ |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 156 | tlv320aic23b_write(sd, 6, 0x00A); |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 157 | /* left-justified, 24-bit, master mode */ |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 158 | tlv320aic23b_write(sd, 7, 0x049); |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 159 | /* set gain on both channels to +3.0 dB */ |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 160 | tlv320aic23b_write(sd, 0, 0x119); |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 161 | /* set sample rate to 48 kHz */ |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 162 | tlv320aic23b_write(sd, 8, 0x000); |
Hans Verkuil | 574dec6 | 2007-11-01 07:58:17 -0300 | [diff] [blame] | 163 | /* activate digital interface */ |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 164 | tlv320aic23b_write(sd, 9, 0x001); |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 165 | |
| 166 | v4l2_ctrl_handler_init(&state->hdl, 1); |
| 167 | v4l2_ctrl_new_std(&state->hdl, &tlv320aic23b_ctrl_ops, |
| 168 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0); |
| 169 | sd->ctrl_handler = &state->hdl; |
| 170 | if (state->hdl.error) { |
| 171 | int err = state->hdl.error; |
| 172 | |
| 173 | v4l2_ctrl_handler_free(&state->hdl); |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 174 | return err; |
| 175 | } |
| 176 | v4l2_ctrl_handler_setup(&state->hdl); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
Hans Verkuil | 6235168 | 2007-09-14 05:03:17 -0300 | [diff] [blame] | 180 | static int tlv320aic23b_remove(struct i2c_client *client) |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 181 | { |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 182 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 183 | struct tlv320aic23b_state *state = to_state(sd); |
Hans Verkuil | 6869ffe | 2008-12-18 12:43:56 -0300 | [diff] [blame] | 184 | |
| 185 | v4l2_device_unregister_subdev(sd); |
Hans Verkuil | e3d5ef0 | 2010-12-12 08:45:04 -0300 | [diff] [blame] | 186 | v4l2_ctrl_handler_free(&state->hdl); |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* ----------------------------------------------------------------------- */ |
| 191 | |
Jean Delvare | ae42908 | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 192 | static const struct i2c_device_id tlv320aic23b_id[] = { |
| 193 | { "tlv320aic23b", 0 }, |
| 194 | { } |
| 195 | }; |
| 196 | MODULE_DEVICE_TABLE(i2c, tlv320aic23b_id); |
Hans Verkuil | 6235168 | 2007-09-14 05:03:17 -0300 | [diff] [blame] | 197 | |
Hans Verkuil | 8e71ff0 | 2010-09-15 15:33:43 -0300 | [diff] [blame] | 198 | static struct i2c_driver tlv320aic23b_driver = { |
| 199 | .driver = { |
Hans Verkuil | 8e71ff0 | 2010-09-15 15:33:43 -0300 | [diff] [blame] | 200 | .name = "tlv320aic23b", |
| 201 | }, |
| 202 | .probe = tlv320aic23b_probe, |
| 203 | .remove = tlv320aic23b_remove, |
| 204 | .id_table = tlv320aic23b_id, |
Scott Alfter | 88ca8ed | 2006-05-20 16:04:31 -0300 | [diff] [blame] | 205 | }; |
Hans Verkuil | 8e71ff0 | 2010-09-15 15:33:43 -0300 | [diff] [blame] | 206 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 207 | module_i2c_driver(tlv320aic23b_driver); |