Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
0004  * Copyright (C) 2007 Hans Verkuil
0005  */
0006 
0007 
0008 #include <linux/module.h>
0009 #include <linux/kernel.h>
0010 #include <linux/i2c.h>
0011 #include <linux/videodev2.h>
0012 #include <linux/slab.h>
0013 #include <media/v4l2-device.h>
0014 #include <media/v4l2-ctrls.h>
0015 
0016 MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
0017 MODULE_AUTHOR("Hans Verkuil");
0018 MODULE_LICENSE("GPL");
0019 
0020 static bool debug;
0021 
0022 module_param(debug, bool, 0644);
0023 
0024 MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
0025 
0026 struct cs5345_state {
0027     struct v4l2_subdev sd;
0028     struct v4l2_ctrl_handler hdl;
0029 };
0030 
0031 static inline struct cs5345_state *to_state(struct v4l2_subdev *sd)
0032 {
0033     return container_of(sd, struct cs5345_state, sd);
0034 }
0035 
0036 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
0037 {
0038     return &container_of(ctrl->handler, struct cs5345_state, hdl)->sd;
0039 }
0040 
0041 /* ----------------------------------------------------------------------- */
0042 
0043 static inline int cs5345_write(struct v4l2_subdev *sd, u8 reg, u8 value)
0044 {
0045     struct i2c_client *client = v4l2_get_subdevdata(sd);
0046 
0047     return i2c_smbus_write_byte_data(client, reg, value);
0048 }
0049 
0050 static inline int cs5345_read(struct v4l2_subdev *sd, u8 reg)
0051 {
0052     struct i2c_client *client = v4l2_get_subdevdata(sd);
0053 
0054     return i2c_smbus_read_byte_data(client, reg);
0055 }
0056 
0057 static int cs5345_s_routing(struct v4l2_subdev *sd,
0058                 u32 input, u32 output, u32 config)
0059 {
0060     if ((input & 0xf) > 6) {
0061         v4l2_err(sd, "Invalid input %d.\n", input);
0062         return -EINVAL;
0063     }
0064     cs5345_write(sd, 0x09, input & 0xf);
0065     cs5345_write(sd, 0x05, input & 0xf0);
0066     return 0;
0067 }
0068 
0069 static int cs5345_s_ctrl(struct v4l2_ctrl *ctrl)
0070 {
0071     struct v4l2_subdev *sd = to_sd(ctrl);
0072 
0073     switch (ctrl->id) {
0074     case V4L2_CID_AUDIO_MUTE:
0075         cs5345_write(sd, 0x04, ctrl->val ? 0x80 : 0);
0076         return 0;
0077     case V4L2_CID_AUDIO_VOLUME:
0078         cs5345_write(sd, 0x07, ((u8)ctrl->val) & 0x3f);
0079         cs5345_write(sd, 0x08, ((u8)ctrl->val) & 0x3f);
0080         return 0;
0081     }
0082     return -EINVAL;
0083 }
0084 
0085 #ifdef CONFIG_VIDEO_ADV_DEBUG
0086 static int cs5345_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
0087 {
0088     reg->size = 1;
0089     reg->val = cs5345_read(sd, reg->reg & 0x1f);
0090     return 0;
0091 }
0092 
0093 static int cs5345_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
0094 {
0095     cs5345_write(sd, reg->reg & 0x1f, reg->val & 0xff);
0096     return 0;
0097 }
0098 #endif
0099 
0100 static int cs5345_log_status(struct v4l2_subdev *sd)
0101 {
0102     u8 v = cs5345_read(sd, 0x09) & 7;
0103     u8 m = cs5345_read(sd, 0x04);
0104     int vol = cs5345_read(sd, 0x08) & 0x3f;
0105 
0106     v4l2_info(sd, "Input:  %d%s\n", v,
0107             (m & 0x80) ? " (muted)" : "");
0108     if (vol >= 32)
0109         vol = vol - 64;
0110     v4l2_info(sd, "Volume: %d dB\n", vol);
0111     return 0;
0112 }
0113 
0114 /* ----------------------------------------------------------------------- */
0115 
0116 static const struct v4l2_ctrl_ops cs5345_ctrl_ops = {
0117     .s_ctrl = cs5345_s_ctrl,
0118 };
0119 
0120 static const struct v4l2_subdev_core_ops cs5345_core_ops = {
0121     .log_status = cs5345_log_status,
0122 #ifdef CONFIG_VIDEO_ADV_DEBUG
0123     .g_register = cs5345_g_register,
0124     .s_register = cs5345_s_register,
0125 #endif
0126 };
0127 
0128 static const struct v4l2_subdev_audio_ops cs5345_audio_ops = {
0129     .s_routing = cs5345_s_routing,
0130 };
0131 
0132 static const struct v4l2_subdev_ops cs5345_ops = {
0133     .core = &cs5345_core_ops,
0134     .audio = &cs5345_audio_ops,
0135 };
0136 
0137 /* ----------------------------------------------------------------------- */
0138 
0139 static int cs5345_probe(struct i2c_client *client,
0140             const struct i2c_device_id *id)
0141 {
0142     struct cs5345_state *state;
0143     struct v4l2_subdev *sd;
0144 
0145     /* Check if the adapter supports the needed features */
0146     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0147         return -EIO;
0148 
0149     v4l_info(client, "chip found @ 0x%x (%s)\n",
0150             client->addr << 1, client->adapter->name);
0151 
0152     state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
0153     if (state == NULL)
0154         return -ENOMEM;
0155     sd = &state->sd;
0156     v4l2_i2c_subdev_init(sd, client, &cs5345_ops);
0157 
0158     v4l2_ctrl_handler_init(&state->hdl, 2);
0159     v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
0160             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
0161     v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
0162             V4L2_CID_AUDIO_VOLUME, -24, 24, 1, 0);
0163     sd->ctrl_handler = &state->hdl;
0164     if (state->hdl.error) {
0165         int err = state->hdl.error;
0166 
0167         v4l2_ctrl_handler_free(&state->hdl);
0168         return err;
0169     }
0170     /* set volume/mute */
0171     v4l2_ctrl_handler_setup(&state->hdl);
0172 
0173     cs5345_write(sd, 0x02, 0x00);
0174     cs5345_write(sd, 0x04, 0x01);
0175     cs5345_write(sd, 0x09, 0x01);
0176     return 0;
0177 }
0178 
0179 /* ----------------------------------------------------------------------- */
0180 
0181 static int cs5345_remove(struct i2c_client *client)
0182 {
0183     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0184     struct cs5345_state *state = to_state(sd);
0185 
0186     v4l2_device_unregister_subdev(sd);
0187     v4l2_ctrl_handler_free(&state->hdl);
0188     return 0;
0189 }
0190 
0191 /* ----------------------------------------------------------------------- */
0192 
0193 static const struct i2c_device_id cs5345_id[] = {
0194     { "cs5345", 0 },
0195     { }
0196 };
0197 MODULE_DEVICE_TABLE(i2c, cs5345_id);
0198 
0199 static struct i2c_driver cs5345_driver = {
0200     .driver = {
0201         .name   = "cs5345",
0202     },
0203     .probe      = cs5345_probe,
0204     .remove     = cs5345_remove,
0205     .id_table   = cs5345_id,
0206 };
0207 
0208 module_i2c_driver(cs5345_driver);