1 // SPDX-License-Identifier: GPL-2.0-or-later 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> 11 */ 12 13 #include <linux/module.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <linux/ioctl.h> 17 #include <linux/uaccess.h> 18 #include <linux/i2c.h> 19 #include <linux/videodev2.h> 20 #include <media/v4l2-device.h> 21 #include <media/v4l2-ctrls.h> 22 23 MODULE_DESCRIPTION("tlv320aic23b driver"); 24 MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil"); 25 MODULE_LICENSE("GPL"); 26 27 28 /* ----------------------------------------------------------------------- */ 29 30 struct tlv320aic23b_state { 31 struct v4l2_subdev sd; 32 struct v4l2_ctrl_handler hdl; 33 }; 34 35 static inline struct tlv320aic23b_state *to_state(struct v4l2_subdev *sd) 36 { 37 return container_of(sd, struct tlv320aic23b_state, sd); 38 } 39 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 45 static int tlv320aic23b_write(struct v4l2_subdev *sd, int reg, u16 val) 46 { 47 struct i2c_client *client = v4l2_get_subdevdata(sd); 48 int i; 49 50 if ((reg < 0 || reg > 9) && (reg != 15)) { 51 v4l2_err(sd, "Invalid register R%d\n", reg); 52 return -1; 53 } 54 55 for (i = 0; i < 3; i++) 56 if (i2c_smbus_write_byte_data(client, 57 (reg << 1) | (val >> 8), val & 0xff) == 0) 58 return 0; 59 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg); 60 return -1; 61 } 62 63 static int tlv320aic23b_s_clock_freq(struct v4l2_subdev *sd, u32 freq) 64 { 65 switch (freq) { 66 case 32000: /* set sample rate to 32 kHz */ 67 tlv320aic23b_write(sd, 8, 0x018); 68 break; 69 case 44100: /* set sample rate to 44.1 kHz */ 70 tlv320aic23b_write(sd, 8, 0x022); 71 break; 72 case 48000: /* set sample rate to 48 kHz */ 73 tlv320aic23b_write(sd, 8, 0x000); 74 break; 75 default: 76 return -EINVAL; 77 } 78 return 0; 79 } 80 81 static int tlv320aic23b_s_ctrl(struct v4l2_ctrl *ctrl) 82 { 83 struct v4l2_subdev *sd = to_sd(ctrl); 84 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; 94 } 95 96 static int tlv320aic23b_log_status(struct v4l2_subdev *sd) 97 { 98 struct tlv320aic23b_state *state = to_state(sd); 99 100 v4l2_ctrl_handler_log_status(&state->hdl, sd->name); 101 return 0; 102 } 103 104 /* ----------------------------------------------------------------------- */ 105 106 static const struct v4l2_ctrl_ops tlv320aic23b_ctrl_ops = { 107 .s_ctrl = tlv320aic23b_s_ctrl, 108 }; 109 110 static const struct v4l2_subdev_core_ops tlv320aic23b_core_ops = { 111 .log_status = tlv320aic23b_log_status, 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 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 132 static int tlv320aic23b_probe(struct i2c_client *client) 133 { 134 struct tlv320aic23b_state *state; 135 struct v4l2_subdev *sd; 136 137 /* Check if the adapter supports the needed features */ 138 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 139 return -EIO; 140 141 v4l_info(client, "chip found @ 0x%x (%s)\n", 142 client->addr << 1, client->adapter->name); 143 144 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); 145 if (state == NULL) 146 return -ENOMEM; 147 sd = &state->sd; 148 v4l2_i2c_subdev_init(sd, client, &tlv320aic23b_ops); 149 150 /* Initialize tlv320aic23b */ 151 152 /* RESET */ 153 tlv320aic23b_write(sd, 15, 0x000); 154 /* turn off DAC & mic input */ 155 tlv320aic23b_write(sd, 6, 0x00A); 156 /* left-justified, 24-bit, master mode */ 157 tlv320aic23b_write(sd, 7, 0x049); 158 /* set gain on both channels to +3.0 dB */ 159 tlv320aic23b_write(sd, 0, 0x119); 160 /* set sample rate to 48 kHz */ 161 tlv320aic23b_write(sd, 8, 0x000); 162 /* activate digital interface */ 163 tlv320aic23b_write(sd, 9, 0x001); 164 165 v4l2_ctrl_handler_init(&state->hdl, 1); 166 v4l2_ctrl_new_std(&state->hdl, &tlv320aic23b_ctrl_ops, 167 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0); 168 sd->ctrl_handler = &state->hdl; 169 if (state->hdl.error) { 170 int err = state->hdl.error; 171 172 v4l2_ctrl_handler_free(&state->hdl); 173 return err; 174 } 175 v4l2_ctrl_handler_setup(&state->hdl); 176 return 0; 177 } 178 179 static void tlv320aic23b_remove(struct i2c_client *client) 180 { 181 struct v4l2_subdev *sd = i2c_get_clientdata(client); 182 struct tlv320aic23b_state *state = to_state(sd); 183 184 v4l2_device_unregister_subdev(sd); 185 v4l2_ctrl_handler_free(&state->hdl); 186 } 187 188 /* ----------------------------------------------------------------------- */ 189 190 static const struct i2c_device_id tlv320aic23b_id[] = { 191 { "tlv320aic23b", 0 }, 192 { } 193 }; 194 MODULE_DEVICE_TABLE(i2c, tlv320aic23b_id); 195 196 static struct i2c_driver tlv320aic23b_driver = { 197 .driver = { 198 .name = "tlv320aic23b", 199 }, 200 .probe = tlv320aic23b_probe, 201 .remove = tlv320aic23b_remove, 202 .id_table = tlv320aic23b_id, 203 }; 204 205 module_i2c_driver(tlv320aic23b_driver); 206