blob: 9a411106cfb3a8a00b0bfabe86c9e7347186251c [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans Verkuilf4067fd2005-11-08 21:37:55 -08002/*
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
Hans Verkuilf4067fd2005-11-08 21:37:55 -08007 */
8
9
10#include <linux/module.h>
11#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Hans Verkuilf4067fd2005-11-08 21:37:55 -080013#include <linux/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080014#include <linux/uaccess.h>
Hans Verkuilf4067fd2005-11-08 21:37:55 -080015#include <linux/i2c.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030016#include <linux/videodev2.h>
Hans Verkuil825c6aa2008-11-29 12:45:59 -030017#include <media/v4l2-device.h>
Hans Verkuil251190c2010-04-23 10:39:25 -030018#include <media/v4l2-ctrls.h>
Hans Verkuilf4067fd2005-11-08 21:37:55 -080019
20MODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC");
21MODULE_AUTHOR("Martin Vaughan");
22MODULE_LICENSE("GPL");
23
Rusty Russell90ab5ee2012-01-13 09:32:20 +103024static bool debug;
Hans Verkuilf4067fd2005-11-08 21:37:55 -080025
26module_param(debug, bool, 0644);
27
Niels de Vos61a2d072008-07-31 00:07:23 -070028MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
Hans Verkuilf4067fd2005-11-08 21:37:55 -080029
Hans Verkuilf4067fd2005-11-08 21:37:55 -080030
Hans Verkuil251190c2010-04-23 10:39:25 -030031struct 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
Hans Verkuilf4067fd2005-11-08 21:37:55 -080046/* ----------------------------------------------------------------------- */
47
Hans Verkuil825c6aa2008-11-29 12:45:59 -030048static int cs53l32a_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Hans Verkuilf4067fd2005-11-08 21:37:55 -080049{
Hans Verkuil825c6aa2008-11-29 12:45:59 -030050 struct i2c_client *client = v4l2_get_subdevdata(sd);
51
Hans Verkuilf4067fd2005-11-08 21:37:55 -080052 return i2c_smbus_write_byte_data(client, reg, value);
53}
54
Hans Verkuil825c6aa2008-11-29 12:45:59 -030055static int cs53l32a_read(struct v4l2_subdev *sd, u8 reg)
Hans Verkuilf4067fd2005-11-08 21:37:55 -080056{
Hans Verkuil825c6aa2008-11-29 12:45:59 -030057 struct i2c_client *client = v4l2_get_subdevdata(sd);
58
Hans Verkuilf4067fd2005-11-08 21:37:55 -080059 return i2c_smbus_read_byte_data(client, reg);
60}
61
Hans Verkuil5325b422009-04-02 11:26:22 -030062static int cs53l32a_s_routing(struct v4l2_subdev *sd,
63 u32 input, u32 output, u32 config)
Hans Verkuil825c6aa2008-11-29 12:45:59 -030064{
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. */
Hans Verkuil5325b422009-04-02 11:26:22 -030069 if (input > 2) {
70 v4l2_err(sd, "Invalid input %d.\n", input);
Hans Verkuil825c6aa2008-11-29 12:45:59 -030071 return -EINVAL;
72 }
Hans Verkuil5325b422009-04-02 11:26:22 -030073 cs53l32a_write(sd, 0x01, 0x01 + (input << 4));
Hans Verkuil825c6aa2008-11-29 12:45:59 -030074 return 0;
75}
76
Hans Verkuil251190c2010-04-23 10:39:25 -030077static int cs53l32a_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil825c6aa2008-11-29 12:45:59 -030078{
Hans Verkuil251190c2010-04-23 10:39:25 -030079 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuil825c6aa2008-11-29 12:45:59 -030080
Hans Verkuil251190c2010-04-23 10:39:25 -030081 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);
Hans Verkuil825c6aa2008-11-29 12:45:59 -030088 return 0;
89 }
Hans Verkuil251190c2010-04-23 10:39:25 -030090 return -EINVAL;
Hans Verkuil825c6aa2008-11-29 12:45:59 -030091}
92
Hans Verkuil825c6aa2008-11-29 12:45:59 -030093static int cs53l32a_log_status(struct v4l2_subdev *sd)
94{
Hans Verkuil251190c2010-04-23 10:39:25 -030095 struct cs53l32a_state *state = to_state(sd);
Hans Verkuil825c6aa2008-11-29 12:45:59 -030096 u8 v = cs53l32a_read(sd, 0x01);
Hans Verkuil825c6aa2008-11-29 12:45:59 -030097
Hans Verkuil251190c2010-04-23 10:39:25 -030098 v4l2_info(sd, "Input: %d\n", (v >> 4) & 3);
99 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300100 return 0;
101}
102
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800103/* ----------------------------------------------------------------------- */
104
Hans Verkuil251190c2010-04-23 10:39:25 -0300105static const struct v4l2_ctrl_ops cs53l32a_ctrl_ops = {
106 .s_ctrl = cs53l32a_s_ctrl,
107};
108
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300109static const struct v4l2_subdev_core_ops cs53l32a_core_ops = {
110 .log_status = cs53l32a_log_status,
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300111};
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
122/* ----------------------------------------------------------------------- */
123
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800124/* 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
Jean Delvared2653e92008-04-29 23:11:39 +0200131static int cs53l32a_probe(struct i2c_client *client,
132 const struct i2c_device_id *id)
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800133{
Hans Verkuil251190c2010-04-23 10:39:25 -0300134 struct cs53l32a_state *state;
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300135 struct v4l2_subdev *sd;
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800136 int i;
137
138 /* Check if the adapter supports the needed features */
Hans Verkuile8e6b992007-09-13 11:30:38 -0300139 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Hans Verkuil188f3452007-09-16 10:47:15 -0300140 return -EIO;
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800141
Jean Delvareaf294862008-05-18 20:49:40 +0200142 if (!id)
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400143 strscpy(client->name, "cs53l32a", sizeof(client->name));
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800144
Hans Verkuil4c05de92007-11-01 07:54:57 -0300145 v4l_info(client, "chip found @ 0x%x (%s)\n",
146 client->addr << 1, client->adapter->name);
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800147
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300148 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil251190c2010-04-23 10:39:25 -0300149 if (state == NULL)
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300150 return -ENOMEM;
Hans Verkuil251190c2010-04-23 10:39:25 -0300151 sd = &state->sd;
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300152 v4l2_i2c_subdev_init(sd, client, &cs53l32a_ops);
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800153
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300154 for (i = 1; i <= 7; i++) {
155 u8 v = cs53l32a_read(sd, i);
156
157 v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v);
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800158 }
159
Hans Verkuil251190c2010-04-23 10:39:25 -0300160 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);
Hans Verkuil251190c2010-04-23 10:39:25 -0300170 return err;
171 }
172
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800173 /* Set cs53l32a internal register for Adaptec 2010/2410 setup */
174
Hans Verkuil251190c2010-04-23 10:39:25 -0300175 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);
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800182
183 /* Display results, should be 0x21,0x29,0x30,0x00,0x00,0x00,0x00 */
184
185 for (i = 1; i <= 7; i++) {
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300186 u8 v = cs53l32a_read(sd, i);
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800187
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300188 v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v);
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800189 }
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800190 return 0;
191}
192
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300193static int cs53l32a_remove(struct i2c_client *client)
194{
195 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuil251190c2010-04-23 10:39:25 -0300196 struct cs53l32a_state *state = to_state(sd);
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300197
198 v4l2_device_unregister_subdev(sd);
Hans Verkuil251190c2010-04-23 10:39:25 -0300199 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuil825c6aa2008-11-29 12:45:59 -0300200 return 0;
201}
202
Jean Delvareaf294862008-05-18 20:49:40 +0200203static const struct i2c_device_id cs53l32a_id[] = {
204 { "cs53l32a", 0 },
205 { }
206};
207MODULE_DEVICE_TABLE(i2c, cs53l32a_id);
208
Hans Verkuil50337ace2010-09-15 15:42:38 -0300209static struct i2c_driver cs53l32a_driver = {
210 .driver = {
Hans Verkuil50337ace2010-09-15 15:42:38 -0300211 .name = "cs53l32a",
212 },
213 .probe = cs53l32a_probe,
214 .remove = cs53l32a_remove,
215 .id_table = cs53l32a_id,
Hans Verkuilf4067fd2005-11-08 21:37:55 -0800216};
Hans Verkuil50337ace2010-09-15 15:42:38 -0300217
Axel Linc6e8d862012-02-12 06:56:32 -0300218module_i2c_driver(cs53l32a_driver);