xref: /linux/drivers/media/i2c/cs5345.c (revision 0e2b2a76278153d1ac312b0691cb65dabb9aef3e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
4  * Copyright (C) 2007 Hans Verkuil
5  */
6 
7 
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/i2c.h>
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ctrls.h>
15 
16 MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
17 MODULE_AUTHOR("Hans Verkuil");
18 MODULE_LICENSE("GPL");
19 
20 static bool debug;
21 
22 module_param(debug, bool, 0644);
23 
24 MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
25 
26 struct cs5345_state {
27 	struct v4l2_subdev sd;
28 	struct v4l2_ctrl_handler hdl;
29 };
30 
31 static inline struct cs5345_state *to_state(struct v4l2_subdev *sd)
32 {
33 	return container_of(sd, struct cs5345_state, sd);
34 }
35 
36 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
37 {
38 	return &container_of(ctrl->handler, struct cs5345_state, hdl)->sd;
39 }
40 
41 /* ----------------------------------------------------------------------- */
42 
43 static inline int cs5345_write(struct v4l2_subdev *sd, u8 reg, u8 value)
44 {
45 	struct i2c_client *client = v4l2_get_subdevdata(sd);
46 
47 	return i2c_smbus_write_byte_data(client, reg, value);
48 }
49 
50 static inline int cs5345_read(struct v4l2_subdev *sd, u8 reg)
51 {
52 	struct i2c_client *client = v4l2_get_subdevdata(sd);
53 
54 	return i2c_smbus_read_byte_data(client, reg);
55 }
56 
57 static int cs5345_s_routing(struct v4l2_subdev *sd,
58 			    u32 input, u32 output, u32 config)
59 {
60 	if ((input & 0xf) > 6) {
61 		v4l2_err(sd, "Invalid input %d.\n", input);
62 		return -EINVAL;
63 	}
64 	cs5345_write(sd, 0x09, input & 0xf);
65 	cs5345_write(sd, 0x05, input & 0xf0);
66 	return 0;
67 }
68 
69 static int cs5345_s_ctrl(struct v4l2_ctrl *ctrl)
70 {
71 	struct v4l2_subdev *sd = to_sd(ctrl);
72 
73 	switch (ctrl->id) {
74 	case V4L2_CID_AUDIO_MUTE:
75 		cs5345_write(sd, 0x04, ctrl->val ? 0x80 : 0);
76 		return 0;
77 	case V4L2_CID_AUDIO_VOLUME:
78 		cs5345_write(sd, 0x07, ((u8)ctrl->val) & 0x3f);
79 		cs5345_write(sd, 0x08, ((u8)ctrl->val) & 0x3f);
80 		return 0;
81 	}
82 	return -EINVAL;
83 }
84 
85 #ifdef CONFIG_VIDEO_ADV_DEBUG
86 static int cs5345_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
87 {
88 	reg->size = 1;
89 	reg->val = cs5345_read(sd, reg->reg & 0x1f);
90 	return 0;
91 }
92 
93 static int cs5345_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
94 {
95 	cs5345_write(sd, reg->reg & 0x1f, reg->val & 0xff);
96 	return 0;
97 }
98 #endif
99 
100 static int cs5345_log_status(struct v4l2_subdev *sd)
101 {
102 	u8 v = cs5345_read(sd, 0x09) & 7;
103 	u8 m = cs5345_read(sd, 0x04);
104 	int vol = cs5345_read(sd, 0x08) & 0x3f;
105 
106 	v4l2_info(sd, "Input:  %d%s\n", v,
107 			(m & 0x80) ? " (muted)" : "");
108 	if (vol >= 32)
109 		vol = vol - 64;
110 	v4l2_info(sd, "Volume: %d dB\n", vol);
111 	return 0;
112 }
113 
114 /* ----------------------------------------------------------------------- */
115 
116 static const struct v4l2_ctrl_ops cs5345_ctrl_ops = {
117 	.s_ctrl = cs5345_s_ctrl,
118 };
119 
120 static const struct v4l2_subdev_core_ops cs5345_core_ops = {
121 	.log_status = cs5345_log_status,
122 #ifdef CONFIG_VIDEO_ADV_DEBUG
123 	.g_register = cs5345_g_register,
124 	.s_register = cs5345_s_register,
125 #endif
126 };
127 
128 static const struct v4l2_subdev_audio_ops cs5345_audio_ops = {
129 	.s_routing = cs5345_s_routing,
130 };
131 
132 static const struct v4l2_subdev_ops cs5345_ops = {
133 	.core = &cs5345_core_ops,
134 	.audio = &cs5345_audio_ops,
135 };
136 
137 /* ----------------------------------------------------------------------- */
138 
139 static int cs5345_probe(struct i2c_client *client)
140 {
141 	struct cs5345_state *state;
142 	struct v4l2_subdev *sd;
143 
144 	/* Check if the adapter supports the needed features */
145 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
146 		return -EIO;
147 
148 	v4l_info(client, "chip found @ 0x%x (%s)\n",
149 			client->addr << 1, client->adapter->name);
150 
151 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
152 	if (state == NULL)
153 		return -ENOMEM;
154 	sd = &state->sd;
155 	v4l2_i2c_subdev_init(sd, client, &cs5345_ops);
156 
157 	v4l2_ctrl_handler_init(&state->hdl, 2);
158 	v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
159 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
160 	v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
161 			V4L2_CID_AUDIO_VOLUME, -24, 24, 1, 0);
162 	sd->ctrl_handler = &state->hdl;
163 	if (state->hdl.error) {
164 		int err = state->hdl.error;
165 
166 		v4l2_ctrl_handler_free(&state->hdl);
167 		return err;
168 	}
169 	/* set volume/mute */
170 	v4l2_ctrl_handler_setup(&state->hdl);
171 
172 	cs5345_write(sd, 0x02, 0x00);
173 	cs5345_write(sd, 0x04, 0x01);
174 	cs5345_write(sd, 0x09, 0x01);
175 	return 0;
176 }
177 
178 /* ----------------------------------------------------------------------- */
179 
180 static void cs5345_remove(struct i2c_client *client)
181 {
182 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
183 	struct cs5345_state *state = to_state(sd);
184 
185 	v4l2_device_unregister_subdev(sd);
186 	v4l2_ctrl_handler_free(&state->hdl);
187 }
188 
189 /* ----------------------------------------------------------------------- */
190 
191 static const struct i2c_device_id cs5345_id[] = {
192 	{ "cs5345", 0 },
193 	{ }
194 };
195 MODULE_DEVICE_TABLE(i2c, cs5345_id);
196 
197 static struct i2c_driver cs5345_driver = {
198 	.driver = {
199 		.name	= "cs5345",
200 	},
201 	.probe		= cs5345_probe,
202 	.remove		= cs5345_remove,
203 	.id_table	= cs5345_id,
204 };
205 
206 module_i2c_driver(cs5345_driver);
207