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