gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / i2c / cs5345.c
blobf6dd5edf77dd9ff1cfdcb7f428c7736274a80535
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 */
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>
16 MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
17 MODULE_AUTHOR("Hans Verkuil");
18 MODULE_LICENSE("GPL");
20 static bool debug;
22 module_param(debug, bool, 0644);
24 MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
26 struct cs5345_state {
27 struct v4l2_subdev sd;
28 struct v4l2_ctrl_handler hdl;
31 static inline struct cs5345_state *to_state(struct v4l2_subdev *sd)
33 return container_of(sd, struct cs5345_state, sd);
36 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
38 return &container_of(ctrl->handler, struct cs5345_state, hdl)->sd;
41 /* ----------------------------------------------------------------------- */
43 static inline int cs5345_write(struct v4l2_subdev *sd, u8 reg, u8 value)
45 struct i2c_client *client = v4l2_get_subdevdata(sd);
47 return i2c_smbus_write_byte_data(client, reg, value);
50 static inline int cs5345_read(struct v4l2_subdev *sd, u8 reg)
52 struct i2c_client *client = v4l2_get_subdevdata(sd);
54 return i2c_smbus_read_byte_data(client, reg);
57 static int cs5345_s_routing(struct v4l2_subdev *sd,
58 u32 input, u32 output, u32 config)
60 if ((input & 0xf) > 6) {
61 v4l2_err(sd, "Invalid input %d.\n", input);
62 return -EINVAL;
64 cs5345_write(sd, 0x09, input & 0xf);
65 cs5345_write(sd, 0x05, input & 0xf0);
66 return 0;
69 static int cs5345_s_ctrl(struct v4l2_ctrl *ctrl)
71 struct v4l2_subdev *sd = to_sd(ctrl);
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;
82 return -EINVAL;
85 #ifdef CONFIG_VIDEO_ADV_DEBUG
86 static int cs5345_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
88 reg->size = 1;
89 reg->val = cs5345_read(sd, reg->reg & 0x1f);
90 return 0;
93 static int cs5345_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
95 cs5345_write(sd, reg->reg & 0x1f, reg->val & 0xff);
96 return 0;
98 #endif
100 static int cs5345_log_status(struct v4l2_subdev *sd)
102 u8 v = cs5345_read(sd, 0x09) & 7;
103 u8 m = cs5345_read(sd, 0x04);
104 int vol = cs5345_read(sd, 0x08) & 0x3f;
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;
114 /* ----------------------------------------------------------------------- */
116 static const struct v4l2_ctrl_ops cs5345_ctrl_ops = {
117 .s_ctrl = cs5345_s_ctrl,
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
128 static const struct v4l2_subdev_audio_ops cs5345_audio_ops = {
129 .s_routing = cs5345_s_routing,
132 static const struct v4l2_subdev_ops cs5345_ops = {
133 .core = &cs5345_core_ops,
134 .audio = &cs5345_audio_ops,
137 /* ----------------------------------------------------------------------- */
139 static int cs5345_probe(struct i2c_client *client,
140 const struct i2c_device_id *id)
142 struct cs5345_state *state;
143 struct v4l2_subdev *sd;
145 /* Check if the adapter supports the needed features */
146 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
147 return -EIO;
149 v4l_info(client, "chip found @ 0x%x (%s)\n",
150 client->addr << 1, client->adapter->name);
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);
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;
167 v4l2_ctrl_handler_free(&state->hdl);
168 return err;
170 /* set volume/mute */
171 v4l2_ctrl_handler_setup(&state->hdl);
173 cs5345_write(sd, 0x02, 0x00);
174 cs5345_write(sd, 0x04, 0x01);
175 cs5345_write(sd, 0x09, 0x01);
176 return 0;
179 /* ----------------------------------------------------------------------- */
181 static int cs5345_remove(struct i2c_client *client)
183 struct v4l2_subdev *sd = i2c_get_clientdata(client);
184 struct cs5345_state *state = to_state(sd);
186 v4l2_device_unregister_subdev(sd);
187 v4l2_ctrl_handler_free(&state->hdl);
188 return 0;
191 /* ----------------------------------------------------------------------- */
193 static const struct i2c_device_id cs5345_id[] = {
194 { "cs5345", 0 },
197 MODULE_DEVICE_TABLE(i2c, cs5345_id);
199 static struct i2c_driver cs5345_driver = {
200 .driver = {
201 .name = "cs5345",
203 .probe = cs5345_probe,
204 .remove = cs5345_remove,
205 .id_table = cs5345_id,
208 module_i2c_driver(cs5345_driver);