blob: f6dd5edf77dd9ff1cfdcb7f428c7736274a80535 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans Verkuil6fb377f2007-12-18 19:40:44 -03002/*
3 * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
4 * Copyright (C) 2007 Hans Verkuil
Hans Verkuil6fb377f2007-12-18 19:40:44 -03005 */
6
7
Hans Verkuil6fb377f2007-12-18 19:40:44 -03008#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/i2c.h>
11#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030013#include <media/v4l2-device.h>
Hans Verkuil34a078d2010-12-12 07:53:28 -030014#include <media/v4l2-ctrls.h>
Hans Verkuil6fb377f2007-12-18 19:40:44 -030015
16MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
17MODULE_AUTHOR("Hans Verkuil");
18MODULE_LICENSE("GPL");
19
Rusty Russell90ab5ee2012-01-13 09:32:20 +103020static bool debug;
Hans Verkuil6fb377f2007-12-18 19:40:44 -030021
22module_param(debug, bool, 0644);
23
Niels de Vos61a2d072008-07-31 00:07:23 -070024MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
Hans Verkuil6fb377f2007-12-18 19:40:44 -030025
Hans Verkuil34a078d2010-12-12 07:53:28 -030026struct cs5345_state {
27 struct v4l2_subdev sd;
28 struct v4l2_ctrl_handler hdl;
29};
30
31static inline struct cs5345_state *to_state(struct v4l2_subdev *sd)
32{
33 return container_of(sd, struct cs5345_state, sd);
34}
35
36static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
37{
38 return &container_of(ctrl->handler, struct cs5345_state, hdl)->sd;
39}
Hans Verkuil6fb377f2007-12-18 19:40:44 -030040
41/* ----------------------------------------------------------------------- */
42
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030043static inline int cs5345_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Hans Verkuil6fb377f2007-12-18 19:40:44 -030044{
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030045 struct i2c_client *client = v4l2_get_subdevdata(sd);
46
Hans Verkuil6fb377f2007-12-18 19:40:44 -030047 return i2c_smbus_write_byte_data(client, reg, value);
48}
49
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030050static inline int cs5345_read(struct v4l2_subdev *sd, u8 reg)
Hans Verkuil6fb377f2007-12-18 19:40:44 -030051{
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030052 struct i2c_client *client = v4l2_get_subdevdata(sd);
53
Hans Verkuil6fb377f2007-12-18 19:40:44 -030054 return i2c_smbus_read_byte_data(client, reg);
55}
56
Hans Verkuil5325b422009-04-02 11:26:22 -030057static int cs5345_s_routing(struct v4l2_subdev *sd,
58 u32 input, u32 output, u32 config)
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030059{
Hans Verkuil5325b422009-04-02 11:26:22 -030060 if ((input & 0xf) > 6) {
61 v4l2_err(sd, "Invalid input %d.\n", input);
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030062 return -EINVAL;
63 }
Hans Verkuil5325b422009-04-02 11:26:22 -030064 cs5345_write(sd, 0x09, input & 0xf);
65 cs5345_write(sd, 0x05, input & 0xf0);
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030066 return 0;
67}
68
Hans Verkuil34a078d2010-12-12 07:53:28 -030069static int cs5345_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030070{
Hans Verkuil34a078d2010-12-12 07:53:28 -030071 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030072
Hans Verkuil34a078d2010-12-12 07:53:28 -030073 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);
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030080 return 0;
81 }
Hans Verkuil34a078d2010-12-12 07:53:28 -030082 return -EINVAL;
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030083}
84
85#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -030086static int cs5345_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030087{
Hans Verkuilaecde8b52008-12-30 07:14:19 -030088 reg->size = 1;
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030089 reg->val = cs5345_read(sd, reg->reg & 0x1f);
90 return 0;
91}
92
Hans Verkuil977ba3b12013-03-24 08:28:46 -030093static int cs5345_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030094{
Hans Verkuilb3ee7e22008-12-19 10:34:22 -030095 cs5345_write(sd, reg->reg & 0x1f, reg->val & 0xff);
96 return 0;
97}
98#endif
99
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300100static 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
Hans Verkuil6fb377f2007-12-18 19:40:44 -0300114/* ----------------------------------------------------------------------- */
115
Hans Verkuil34a078d2010-12-12 07:53:28 -0300116static const struct v4l2_ctrl_ops cs5345_ctrl_ops = {
117 .s_ctrl = cs5345_s_ctrl,
118};
119
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300120static const struct v4l2_subdev_core_ops cs5345_core_ops = {
121 .log_status = cs5345_log_status,
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300122#ifdef CONFIG_VIDEO_ADV_DEBUG
123 .g_register = cs5345_g_register,
124 .s_register = cs5345_s_register,
125#endif
126};
127
128static const struct v4l2_subdev_audio_ops cs5345_audio_ops = {
129 .s_routing = cs5345_s_routing,
130};
131
132static const struct v4l2_subdev_ops cs5345_ops = {
133 .core = &cs5345_core_ops,
134 .audio = &cs5345_audio_ops,
135};
136
137/* ----------------------------------------------------------------------- */
138
Jean Delvared2653e92008-04-29 23:11:39 +0200139static int cs5345_probe(struct i2c_client *client,
140 const struct i2c_device_id *id)
Hans Verkuil6fb377f2007-12-18 19:40:44 -0300141{
Hans Verkuil34a078d2010-12-12 07:53:28 -0300142 struct cs5345_state *state;
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300143 struct v4l2_subdev *sd;
144
Hans Verkuil6fb377f2007-12-18 19:40:44 -0300145 /* 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
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300152 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil34a078d2010-12-12 07:53:28 -0300153 if (state == NULL)
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300154 return -ENOMEM;
Hans Verkuil34a078d2010-12-12 07:53:28 -0300155 sd = &state->sd;
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300156 v4l2_i2c_subdev_init(sd, client, &cs5345_ops);
157
Hans Verkuil34a078d2010-12-12 07:53:28 -0300158 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);
Hans Verkuil34a078d2010-12-12 07:53:28 -0300168 return err;
169 }
170 /* set volume/mute */
171 v4l2_ctrl_handler_setup(&state->hdl);
172
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300173 cs5345_write(sd, 0x02, 0x00);
174 cs5345_write(sd, 0x04, 0x01);
175 cs5345_write(sd, 0x09, 0x01);
176 return 0;
177}
178
179/* ----------------------------------------------------------------------- */
180
181static int cs5345_remove(struct i2c_client *client)
182{
183 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuil34a078d2010-12-12 07:53:28 -0300184 struct cs5345_state *state = to_state(sd);
Hans Verkuilb3ee7e22008-12-19 10:34:22 -0300185
186 v4l2_device_unregister_subdev(sd);
Hans Verkuil34a078d2010-12-12 07:53:28 -0300187 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuil6fb377f2007-12-18 19:40:44 -0300188 return 0;
189}
190
191/* ----------------------------------------------------------------------- */
192
Jean Delvareaf294862008-05-18 20:49:40 +0200193static const struct i2c_device_id cs5345_id[] = {
194 { "cs5345", 0 },
195 { }
196};
197MODULE_DEVICE_TABLE(i2c, cs5345_id);
198
Hans Verkuil51e86842010-09-15 15:00:07 -0300199static struct i2c_driver cs5345_driver = {
200 .driver = {
Hans Verkuil51e86842010-09-15 15:00:07 -0300201 .name = "cs5345",
202 },
203 .probe = cs5345_probe,
204 .remove = cs5345_remove,
205 .id_table = cs5345_id,
Hans Verkuil6fb377f2007-12-18 19:40:44 -0300206};
Hans Verkuil51e86842010-09-15 15:00:07 -0300207
Axel Linc6e8d862012-02-12 06:56:32 -0300208module_i2c_driver(cs5345_driver);