Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * tlv320aic23b - driver version 0.0.1
0004  *
0005  * Copyright (C) 2006 Scott Alfter <salfter@ssai.us>
0006  *
0007  * Based on wm8775 driver
0008  *
0009  * Copyright (C) 2004 Ulf Eklund <ivtv at eklund.to>
0010  * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/types.h>
0015 #include <linux/slab.h>
0016 #include <linux/ioctl.h>
0017 #include <linux/uaccess.h>
0018 #include <linux/i2c.h>
0019 #include <linux/videodev2.h>
0020 #include <media/v4l2-device.h>
0021 #include <media/v4l2-ctrls.h>
0022 
0023 MODULE_DESCRIPTION("tlv320aic23b driver");
0024 MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil");
0025 MODULE_LICENSE("GPL");
0026 
0027 
0028 /* ----------------------------------------------------------------------- */
0029 
0030 struct tlv320aic23b_state {
0031     struct v4l2_subdev sd;
0032     struct v4l2_ctrl_handler hdl;
0033 };
0034 
0035 static inline struct tlv320aic23b_state *to_state(struct v4l2_subdev *sd)
0036 {
0037     return container_of(sd, struct tlv320aic23b_state, sd);
0038 }
0039 
0040 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
0041 {
0042     return &container_of(ctrl->handler, struct tlv320aic23b_state, hdl)->sd;
0043 }
0044 
0045 static int tlv320aic23b_write(struct v4l2_subdev *sd, int reg, u16 val)
0046 {
0047     struct i2c_client *client = v4l2_get_subdevdata(sd);
0048     int i;
0049 
0050     if ((reg < 0 || reg > 9) && (reg != 15)) {
0051         v4l2_err(sd, "Invalid register R%d\n", reg);
0052         return -1;
0053     }
0054 
0055     for (i = 0; i < 3; i++)
0056         if (i2c_smbus_write_byte_data(client,
0057                 (reg << 1) | (val >> 8), val & 0xff) == 0)
0058             return 0;
0059     v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
0060     return -1;
0061 }
0062 
0063 static int tlv320aic23b_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
0064 {
0065     switch (freq) {
0066     case 32000: /* set sample rate to 32 kHz */
0067         tlv320aic23b_write(sd, 8, 0x018);
0068         break;
0069     case 44100: /* set sample rate to 44.1 kHz */
0070         tlv320aic23b_write(sd, 8, 0x022);
0071         break;
0072     case 48000: /* set sample rate to 48 kHz */
0073         tlv320aic23b_write(sd, 8, 0x000);
0074         break;
0075     default:
0076         return -EINVAL;
0077     }
0078     return 0;
0079 }
0080 
0081 static int tlv320aic23b_s_ctrl(struct v4l2_ctrl *ctrl)
0082 {
0083     struct v4l2_subdev *sd = to_sd(ctrl);
0084 
0085     switch (ctrl->id) {
0086     case V4L2_CID_AUDIO_MUTE:
0087         tlv320aic23b_write(sd, 0, 0x180); /* mute both channels */
0088         /* set gain on both channels to +3.0 dB */
0089         if (!ctrl->val)
0090             tlv320aic23b_write(sd, 0, 0x119);
0091         return 0;
0092     }
0093     return -EINVAL;
0094 }
0095 
0096 static int tlv320aic23b_log_status(struct v4l2_subdev *sd)
0097 {
0098     struct tlv320aic23b_state *state = to_state(sd);
0099 
0100     v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
0101     return 0;
0102 }
0103 
0104 /* ----------------------------------------------------------------------- */
0105 
0106 static const struct v4l2_ctrl_ops tlv320aic23b_ctrl_ops = {
0107     .s_ctrl = tlv320aic23b_s_ctrl,
0108 };
0109 
0110 static const struct v4l2_subdev_core_ops tlv320aic23b_core_ops = {
0111     .log_status = tlv320aic23b_log_status,
0112 };
0113 
0114 static const struct v4l2_subdev_audio_ops tlv320aic23b_audio_ops = {
0115     .s_clock_freq = tlv320aic23b_s_clock_freq,
0116 };
0117 
0118 static const struct v4l2_subdev_ops tlv320aic23b_ops = {
0119     .core = &tlv320aic23b_core_ops,
0120     .audio = &tlv320aic23b_audio_ops,
0121 };
0122 
0123 /* ----------------------------------------------------------------------- */
0124 
0125 /* i2c implementation */
0126 
0127 /*
0128  * Generic i2c probe
0129  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
0130  */
0131 
0132 static int tlv320aic23b_probe(struct i2c_client *client,
0133                   const struct i2c_device_id *id)
0134 {
0135     struct tlv320aic23b_state *state;
0136     struct v4l2_subdev *sd;
0137 
0138     /* Check if the adapter supports the needed features */
0139     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0140         return -EIO;
0141 
0142     v4l_info(client, "chip found @ 0x%x (%s)\n",
0143             client->addr << 1, client->adapter->name);
0144 
0145     state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
0146     if (state == NULL)
0147         return -ENOMEM;
0148     sd = &state->sd;
0149     v4l2_i2c_subdev_init(sd, client, &tlv320aic23b_ops);
0150 
0151     /* Initialize tlv320aic23b */
0152 
0153     /* RESET */
0154     tlv320aic23b_write(sd, 15, 0x000);
0155     /* turn off DAC & mic input */
0156     tlv320aic23b_write(sd, 6, 0x00A);
0157     /* left-justified, 24-bit, master mode */
0158     tlv320aic23b_write(sd, 7, 0x049);
0159     /* set gain on both channels to +3.0 dB */
0160     tlv320aic23b_write(sd, 0, 0x119);
0161     /* set sample rate to 48 kHz */
0162     tlv320aic23b_write(sd, 8, 0x000);
0163     /* activate digital interface */
0164     tlv320aic23b_write(sd, 9, 0x001);
0165 
0166     v4l2_ctrl_handler_init(&state->hdl, 1);
0167     v4l2_ctrl_new_std(&state->hdl, &tlv320aic23b_ctrl_ops,
0168             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
0169     sd->ctrl_handler = &state->hdl;
0170     if (state->hdl.error) {
0171         int err = state->hdl.error;
0172 
0173         v4l2_ctrl_handler_free(&state->hdl);
0174         return err;
0175     }
0176     v4l2_ctrl_handler_setup(&state->hdl);
0177     return 0;
0178 }
0179 
0180 static int tlv320aic23b_remove(struct i2c_client *client)
0181 {
0182     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0183     struct tlv320aic23b_state *state = to_state(sd);
0184 
0185     v4l2_device_unregister_subdev(sd);
0186     v4l2_ctrl_handler_free(&state->hdl);
0187     return 0;
0188 }
0189 
0190 /* ----------------------------------------------------------------------- */
0191 
0192 static const struct i2c_device_id tlv320aic23b_id[] = {
0193     { "tlv320aic23b", 0 },
0194     { }
0195 };
0196 MODULE_DEVICE_TABLE(i2c, tlv320aic23b_id);
0197 
0198 static struct i2c_driver tlv320aic23b_driver = {
0199     .driver = {
0200         .name   = "tlv320aic23b",
0201     },
0202     .probe      = tlv320aic23b_probe,
0203     .remove     = tlv320aic23b_remove,
0204     .id_table   = tlv320aic23b_id,
0205 };
0206 
0207 module_i2c_driver(tlv320aic23b_driver);