blob: e4c21990fea99dcf7e166f04a4b1601666bf045c [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Scott Alfter88ca8ed2006-05-20 16:04:31 -03002/*
3 * tlv320aic23b - driver version 0.0.1
4 *
5 * Copyright (C) 2006 Scott Alfter <salfter@ssai.us>
6 *
7 * Based on wm8775 driver
8 *
9 * Copyright (C) 2004 Ulf Eklund <ivtv at eklund.to>
10 * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
Scott Alfter88ca8ed2006-05-20 16:04:31 -030011 */
12
13#include <linux/module.h>
14#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Scott Alfter88ca8ed2006-05-20 16:04:31 -030016#include <linux/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
Scott Alfter88ca8ed2006-05-20 16:04:31 -030018#include <linux/i2c.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030019#include <linux/videodev2.h>
Hans Verkuil6869ffe2008-12-18 12:43:56 -030020#include <media/v4l2-device.h>
Hans Verkuile3d5ef02010-12-12 08:45:04 -030021#include <media/v4l2-ctrls.h>
Scott Alfter88ca8ed2006-05-20 16:04:31 -030022
23MODULE_DESCRIPTION("tlv320aic23b driver");
24MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil");
25MODULE_LICENSE("GPL");
26
Scott Alfter88ca8ed2006-05-20 16:04:31 -030027
28/* ----------------------------------------------------------------------- */
29
30struct tlv320aic23b_state {
Hans Verkuil6869ffe2008-12-18 12:43:56 -030031 struct v4l2_subdev sd;
Hans Verkuile3d5ef02010-12-12 08:45:04 -030032 struct v4l2_ctrl_handler hdl;
Scott Alfter88ca8ed2006-05-20 16:04:31 -030033};
34
Hans Verkuil6869ffe2008-12-18 12:43:56 -030035static inline struct tlv320aic23b_state *to_state(struct v4l2_subdev *sd)
Scott Alfter88ca8ed2006-05-20 16:04:31 -030036{
Hans Verkuil6869ffe2008-12-18 12:43:56 -030037 return container_of(sd, struct tlv320aic23b_state, sd);
38}
39
Hans Verkuile3d5ef02010-12-12 08:45:04 -030040static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
41{
42 return &container_of(ctrl->handler, struct tlv320aic23b_state, hdl)->sd;
43}
44
Hans Verkuil6869ffe2008-12-18 12:43:56 -030045static int tlv320aic23b_write(struct v4l2_subdev *sd, int reg, u16 val)
46{
47 struct i2c_client *client = v4l2_get_subdevdata(sd);
Scott Alfter88ca8ed2006-05-20 16:04:31 -030048 int i;
49
50 if ((reg < 0 || reg > 9) && (reg != 15)) {
Hans Verkuil6869ffe2008-12-18 12:43:56 -030051 v4l2_err(sd, "Invalid register R%d\n", reg);
Scott Alfter88ca8ed2006-05-20 16:04:31 -030052 return -1;
53 }
54
Hans Verkuil574dec62007-11-01 07:58:17 -030055 for (i = 0; i < 3; i++)
56 if (i2c_smbus_write_byte_data(client,
57 (reg << 1) | (val >> 8), val & 0xff) == 0)
Scott Alfter88ca8ed2006-05-20 16:04:31 -030058 return 0;
Hans Verkuil6869ffe2008-12-18 12:43:56 -030059 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
Scott Alfter88ca8ed2006-05-20 16:04:31 -030060 return -1;
61}
62
Hans Verkuil6869ffe2008-12-18 12:43:56 -030063static int tlv320aic23b_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
Scott Alfter88ca8ed2006-05-20 16:04:31 -030064{
Hans Verkuil6869ffe2008-12-18 12:43:56 -030065 switch (freq) {
66 case 32000: /* set sample rate to 32 kHz */
67 tlv320aic23b_write(sd, 8, 0x018);
Scott Alfter88ca8ed2006-05-20 16:04:31 -030068 break;
Hans Verkuil6869ffe2008-12-18 12:43:56 -030069 case 44100: /* set sample rate to 44.1 kHz */
70 tlv320aic23b_write(sd, 8, 0x022);
Scott Alfter88ca8ed2006-05-20 16:04:31 -030071 break;
Hans Verkuil6869ffe2008-12-18 12:43:56 -030072 case 48000: /* set sample rate to 48 kHz */
73 tlv320aic23b_write(sd, 8, 0x000);
Scott Alfter88ca8ed2006-05-20 16:04:31 -030074 break;
Scott Alfter88ca8ed2006-05-20 16:04:31 -030075 default:
76 return -EINVAL;
77 }
78 return 0;
79}
80
Hans Verkuile3d5ef02010-12-12 08:45:04 -030081static int tlv320aic23b_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil6869ffe2008-12-18 12:43:56 -030082{
Hans Verkuile3d5ef02010-12-12 08:45:04 -030083 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuil6869ffe2008-12-18 12:43:56 -030084
Hans Verkuile3d5ef02010-12-12 08:45:04 -030085 switch (ctrl->id) {
86 case V4L2_CID_AUDIO_MUTE:
87 tlv320aic23b_write(sd, 0, 0x180); /* mute both channels */
88 /* set gain on both channels to +3.0 dB */
89 if (!ctrl->val)
90 tlv320aic23b_write(sd, 0, 0x119);
91 return 0;
92 }
93 return -EINVAL;
Hans Verkuil6869ffe2008-12-18 12:43:56 -030094}
95
96static int tlv320aic23b_log_status(struct v4l2_subdev *sd)
97{
98 struct tlv320aic23b_state *state = to_state(sd);
99
Hans Verkuile3d5ef02010-12-12 08:45:04 -0300100 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300101 return 0;
102}
103
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300104/* ----------------------------------------------------------------------- */
105
Hans Verkuile3d5ef02010-12-12 08:45:04 -0300106static const struct v4l2_ctrl_ops tlv320aic23b_ctrl_ops = {
107 .s_ctrl = tlv320aic23b_s_ctrl,
108};
109
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300110static const struct v4l2_subdev_core_ops tlv320aic23b_core_ops = {
111 .log_status = tlv320aic23b_log_status,
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300112};
113
114static const struct v4l2_subdev_audio_ops tlv320aic23b_audio_ops = {
115 .s_clock_freq = tlv320aic23b_s_clock_freq,
116};
117
118static const struct v4l2_subdev_ops tlv320aic23b_ops = {
119 .core = &tlv320aic23b_core_ops,
120 .audio = &tlv320aic23b_audio_ops,
121};
122
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300123/* ----------------------------------------------------------------------- */
124
125/* i2c implementation */
126
127/*
128 * Generic i2c probe
129 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
130 */
131
Jean Delvared2653e92008-04-29 23:11:39 +0200132static int tlv320aic23b_probe(struct i2c_client *client,
133 const struct i2c_device_id *id)
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300134{
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300135 struct tlv320aic23b_state *state;
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300136 struct v4l2_subdev *sd;
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300137
138 /* Check if the adapter supports the needed features */
Hans Verkuil62351682007-09-14 05:03:17 -0300139 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Hans Verkuil188f3452007-09-16 10:47:15 -0300140 return -EIO;
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300141
Hans Verkuil574dec62007-11-01 07:58:17 -0300142 v4l_info(client, "chip found @ 0x%x (%s)\n",
143 client->addr << 1, client->adapter->name);
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300144
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300145 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil574dec62007-11-01 07:58:17 -0300146 if (state == NULL)
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300147 return -ENOMEM;
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300148 sd = &state->sd;
149 v4l2_i2c_subdev_init(sd, client, &tlv320aic23b_ops);
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300150
Hans Verkuil574dec62007-11-01 07:58:17 -0300151 /* Initialize tlv320aic23b */
152
153 /* RESET */
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300154 tlv320aic23b_write(sd, 15, 0x000);
Hans Verkuil574dec62007-11-01 07:58:17 -0300155 /* turn off DAC & mic input */
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300156 tlv320aic23b_write(sd, 6, 0x00A);
Hans Verkuil574dec62007-11-01 07:58:17 -0300157 /* left-justified, 24-bit, master mode */
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300158 tlv320aic23b_write(sd, 7, 0x049);
Hans Verkuil574dec62007-11-01 07:58:17 -0300159 /* set gain on both channels to +3.0 dB */
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300160 tlv320aic23b_write(sd, 0, 0x119);
Hans Verkuil574dec62007-11-01 07:58:17 -0300161 /* set sample rate to 48 kHz */
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300162 tlv320aic23b_write(sd, 8, 0x000);
Hans Verkuil574dec62007-11-01 07:58:17 -0300163 /* activate digital interface */
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300164 tlv320aic23b_write(sd, 9, 0x001);
Hans Verkuile3d5ef02010-12-12 08:45:04 -0300165
166 v4l2_ctrl_handler_init(&state->hdl, 1);
167 v4l2_ctrl_new_std(&state->hdl, &tlv320aic23b_ctrl_ops,
168 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
169 sd->ctrl_handler = &state->hdl;
170 if (state->hdl.error) {
171 int err = state->hdl.error;
172
173 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuile3d5ef02010-12-12 08:45:04 -0300174 return err;
175 }
176 v4l2_ctrl_handler_setup(&state->hdl);
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300177 return 0;
178}
179
Hans Verkuil62351682007-09-14 05:03:17 -0300180static int tlv320aic23b_remove(struct i2c_client *client)
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300181{
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300182 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuile3d5ef02010-12-12 08:45:04 -0300183 struct tlv320aic23b_state *state = to_state(sd);
Hans Verkuil6869ffe2008-12-18 12:43:56 -0300184
185 v4l2_device_unregister_subdev(sd);
Hans Verkuile3d5ef02010-12-12 08:45:04 -0300186 v4l2_ctrl_handler_free(&state->hdl);
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300187 return 0;
188}
189
190/* ----------------------------------------------------------------------- */
191
Jean Delvareae429082008-05-11 20:37:06 +0200192static const struct i2c_device_id tlv320aic23b_id[] = {
193 { "tlv320aic23b", 0 },
194 { }
195};
196MODULE_DEVICE_TABLE(i2c, tlv320aic23b_id);
Hans Verkuil62351682007-09-14 05:03:17 -0300197
Hans Verkuil8e71ff02010-09-15 15:33:43 -0300198static struct i2c_driver tlv320aic23b_driver = {
199 .driver = {
Hans Verkuil8e71ff02010-09-15 15:33:43 -0300200 .name = "tlv320aic23b",
201 },
202 .probe = tlv320aic23b_probe,
203 .remove = tlv320aic23b_remove,
204 .id_table = tlv320aic23b_id,
Scott Alfter88ca8ed2006-05-20 16:04:31 -0300205};
Hans Verkuil8e71ff02010-09-15 15:33:43 -0300206
Axel Linc6e8d862012-02-12 06:56:32 -0300207module_i2c_driver(tlv320aic23b_driver);