xref: /openbmc/linux/drivers/media/i2c/cs5345.c (revision a2cab953)
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 			const struct i2c_device_id *id)
141 {
142 	struct cs5345_state *state;
143 	struct v4l2_subdev *sd;
144 
145 	/* Check if the adapter supports the needed features */
146 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
147 		return -EIO;
148 
149 	v4l_info(client, "chip found @ 0x%x (%s)\n",
150 			client->addr << 1, client->adapter->name);
151 
152 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
153 	if (state == NULL)
154 		return -ENOMEM;
155 	sd = &state->sd;
156 	v4l2_i2c_subdev_init(sd, client, &cs5345_ops);
157 
158 	v4l2_ctrl_handler_init(&state->hdl, 2);
159 	v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
160 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
161 	v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
162 			V4L2_CID_AUDIO_VOLUME, -24, 24, 1, 0);
163 	sd->ctrl_handler = &state->hdl;
164 	if (state->hdl.error) {
165 		int err = state->hdl.error;
166 
167 		v4l2_ctrl_handler_free(&state->hdl);
168 		return err;
169 	}
170 	/* set volume/mute */
171 	v4l2_ctrl_handler_setup(&state->hdl);
172 
173 	cs5345_write(sd, 0x02, 0x00);
174 	cs5345_write(sd, 0x04, 0x01);
175 	cs5345_write(sd, 0x09, 0x01);
176 	return 0;
177 }
178 
179 /* ----------------------------------------------------------------------- */
180 
181 static void cs5345_remove(struct i2c_client *client)
182 {
183 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
184 	struct cs5345_state *state = to_state(sd);
185 
186 	v4l2_device_unregister_subdev(sd);
187 	v4l2_ctrl_handler_free(&state->hdl);
188 }
189 
190 /* ----------------------------------------------------------------------- */
191 
192 static const struct i2c_device_id cs5345_id[] = {
193 	{ "cs5345", 0 },
194 	{ }
195 };
196 MODULE_DEVICE_TABLE(i2c, cs5345_id);
197 
198 static struct i2c_driver cs5345_driver = {
199 	.driver = {
200 		.name	= "cs5345",
201 	},
202 	.probe		= cs5345_probe,
203 	.remove		= cs5345_remove,
204 	.id_table	= cs5345_id,
205 };
206 
207 module_i2c_driver(cs5345_driver);
208