]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/media/i2c/cs53l32a.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[thirdparty/kernel/stable.git] / drivers / media / i2c / cs53l32a.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
f4067fd4
HV
2/*
3 * cs53l32a (Adaptec AVC-2010 and AVC-2410) i2c ivtv driver.
4 * Copyright (C) 2005 Martin Vaughan
5 *
6 * Audio source switching for Adaptec AVC-2410 added by Trev Jackson
f4067fd4
HV
7 */
8
9
10#include <linux/module.h>
11#include <linux/types.h>
5a0e3ad6 12#include <linux/slab.h>
f4067fd4 13#include <linux/ioctl.h>
7c0f6ba6 14#include <linux/uaccess.h>
f4067fd4 15#include <linux/i2c.h>
33b687cf 16#include <linux/videodev2.h>
825c6aa2 17#include <media/v4l2-device.h>
251190cf 18#include <media/v4l2-ctrls.h>
f4067fd4
HV
19
20MODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC");
21MODULE_AUTHOR("Martin Vaughan");
22MODULE_LICENSE("GPL");
23
90ab5ee9 24static bool debug;
f4067fd4
HV
25
26module_param(debug, bool, 0644);
27
61a2d07d 28MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
f4067fd4 29
f4067fd4 30
251190cf
HV
31struct cs53l32a_state {
32 struct v4l2_subdev sd;
33 struct v4l2_ctrl_handler hdl;
34};
35
36static inline struct cs53l32a_state *to_state(struct v4l2_subdev *sd)
37{
38 return container_of(sd, struct cs53l32a_state, sd);
39}
40
41static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
42{
43 return &container_of(ctrl->handler, struct cs53l32a_state, hdl)->sd;
44}
45
f4067fd4
HV
46/* ----------------------------------------------------------------------- */
47
825c6aa2 48static int cs53l32a_write(struct v4l2_subdev *sd, u8 reg, u8 value)
f4067fd4 49{
825c6aa2
HV
50 struct i2c_client *client = v4l2_get_subdevdata(sd);
51
f4067fd4
HV
52 return i2c_smbus_write_byte_data(client, reg, value);
53}
54
825c6aa2 55static int cs53l32a_read(struct v4l2_subdev *sd, u8 reg)
f4067fd4 56{
825c6aa2
HV
57 struct i2c_client *client = v4l2_get_subdevdata(sd);
58
f4067fd4
HV
59 return i2c_smbus_read_byte_data(client, reg);
60}
61
5325b427
HV
62static int cs53l32a_s_routing(struct v4l2_subdev *sd,
63 u32 input, u32 output, u32 config)
f4067fd4 64{
825c6aa2
HV
65 /* There are 2 physical inputs, but the second input can be
66 placed in two modes, the first mode bypasses the PGA (gain),
67 the second goes through the PGA. Hence there are three
68 possible inputs to choose from. */
5325b427
HV
69 if (input > 2) {
70 v4l2_err(sd, "Invalid input %d.\n", input);
f4067fd4
HV
71 return -EINVAL;
72 }
5325b427 73 cs53l32a_write(sd, 0x01, 0x01 + (input << 4));
825c6aa2
HV
74 return 0;
75}
76
251190cf 77static int cs53l32a_s_ctrl(struct v4l2_ctrl *ctrl)
825c6aa2 78{
251190cf 79 struct v4l2_subdev *sd = to_sd(ctrl);
825c6aa2 80
251190cf
HV
81 switch (ctrl->id) {
82 case V4L2_CID_AUDIO_MUTE:
83 cs53l32a_write(sd, 0x03, ctrl->val ? 0xf0 : 0x30);
84 return 0;
85 case V4L2_CID_AUDIO_VOLUME:
86 cs53l32a_write(sd, 0x04, (u8)ctrl->val);
87 cs53l32a_write(sd, 0x05, (u8)ctrl->val);
825c6aa2
HV
88 return 0;
89 }
251190cf 90 return -EINVAL;
f4067fd4
HV
91}
92
825c6aa2
HV
93static int cs53l32a_log_status(struct v4l2_subdev *sd)
94{
251190cf 95 struct cs53l32a_state *state = to_state(sd);
825c6aa2 96 u8 v = cs53l32a_read(sd, 0x01);
825c6aa2 97
251190cf
HV
98 v4l2_info(sd, "Input: %d\n", (v >> 4) & 3);
99 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
825c6aa2
HV
100 return 0;
101}
102
825c6aa2
HV
103/* ----------------------------------------------------------------------- */
104
251190cf
HV
105static const struct v4l2_ctrl_ops cs53l32a_ctrl_ops = {
106 .s_ctrl = cs53l32a_s_ctrl,
107};
108
825c6aa2
HV
109static const struct v4l2_subdev_core_ops cs53l32a_core_ops = {
110 .log_status = cs53l32a_log_status,
825c6aa2
HV
111};
112
113static const struct v4l2_subdev_audio_ops cs53l32a_audio_ops = {
114 .s_routing = cs53l32a_s_routing,
115};
116
117static const struct v4l2_subdev_ops cs53l32a_ops = {
118 .core = &cs53l32a_core_ops,
119 .audio = &cs53l32a_audio_ops,
120};
121
f4067fd4
HV
122/* ----------------------------------------------------------------------- */
123
124/* i2c implementation */
125
126/*
127 * Generic i2c probe
128 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
129 */
130
d2653e92
JD
131static int cs53l32a_probe(struct i2c_client *client,
132 const struct i2c_device_id *id)
f4067fd4 133{
251190cf 134 struct cs53l32a_state *state;
825c6aa2 135 struct v4l2_subdev *sd;
f4067fd4
HV
136 int i;
137
138 /* Check if the adapter supports the needed features */
e8e6b991 139 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
188f3457 140 return -EIO;
f4067fd4 141
af294867 142 if (!id)
c0decac1 143 strscpy(client->name, "cs53l32a", sizeof(client->name));
f4067fd4 144
4c05de9c
HV
145 v4l_info(client, "chip found @ 0x%x (%s)\n",
146 client->addr << 1, client->adapter->name);
f4067fd4 147
c02b211d 148 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
251190cf 149 if (state == NULL)
825c6aa2 150 return -ENOMEM;
251190cf 151 sd = &state->sd;
825c6aa2
HV
152 v4l2_i2c_subdev_init(sd, client, &cs53l32a_ops);
153
f4067fd4 154 for (i = 1; i <= 7; i++) {
825c6aa2 155 u8 v = cs53l32a_read(sd, i);
f4067fd4 156
825c6aa2 157 v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v);
f4067fd4
HV
158 }
159
251190cf
HV
160 v4l2_ctrl_handler_init(&state->hdl, 2);
161 v4l2_ctrl_new_std(&state->hdl, &cs53l32a_ctrl_ops,
162 V4L2_CID_AUDIO_VOLUME, -96, 12, 1, 0);
163 v4l2_ctrl_new_std(&state->hdl, &cs53l32a_ctrl_ops,
164 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
165 sd->ctrl_handler = &state->hdl;
166 if (state->hdl.error) {
167 int err = state->hdl.error;
168
169 v4l2_ctrl_handler_free(&state->hdl);
251190cf
HV
170 return err;
171 }
172
f4067fd4
HV
173 /* Set cs53l32a internal register for Adaptec 2010/2410 setup */
174
251190cf
HV
175 cs53l32a_write(sd, 0x01, 0x21);
176 cs53l32a_write(sd, 0x02, 0x29);
177 cs53l32a_write(sd, 0x03, 0x30);
178 cs53l32a_write(sd, 0x04, 0x00);
179 cs53l32a_write(sd, 0x05, 0x00);
180 cs53l32a_write(sd, 0x06, 0x00);
181 cs53l32a_write(sd, 0x07, 0x00);
f4067fd4
HV
182
183 /* Display results, should be 0x21,0x29,0x30,0x00,0x00,0x00,0x00 */
184
185 for (i = 1; i <= 7; i++) {
825c6aa2 186 u8 v = cs53l32a_read(sd, i);
f4067fd4 187
825c6aa2 188 v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v);
f4067fd4 189 }
f4067fd4
HV
190 return 0;
191}
192
825c6aa2
HV
193static int cs53l32a_remove(struct i2c_client *client)
194{
195 struct v4l2_subdev *sd = i2c_get_clientdata(client);
251190cf 196 struct cs53l32a_state *state = to_state(sd);
825c6aa2
HV
197
198 v4l2_device_unregister_subdev(sd);
251190cf 199 v4l2_ctrl_handler_free(&state->hdl);
825c6aa2
HV
200 return 0;
201}
202
af294867
JD
203static const struct i2c_device_id cs53l32a_id[] = {
204 { "cs53l32a", 0 },
205 { }
206};
207MODULE_DEVICE_TABLE(i2c, cs53l32a_id);
208
50337ace
HV
209static struct i2c_driver cs53l32a_driver = {
210 .driver = {
50337ace
HV
211 .name = "cs53l32a",
212 },
213 .probe = cs53l32a_probe,
214 .remove = cs53l32a_remove,
215 .id_table = cs53l32a_id,
f4067fd4 216};
50337ace 217
c6e8d86f 218module_i2c_driver(cs53l32a_driver);