18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * cs53l32a (Adaptec AVC-2010 and AVC-2410) i2c ivtv driver. 48c2ecf20Sopenharmony_ci * Copyright (C) 2005 Martin Vaughan 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Audio source switching for Adaptec AVC-2410 added by Trev Jackson 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/types.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/ioctl.h> 148c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 158c2ecf20Sopenharmony_ci#include <linux/i2c.h> 168c2ecf20Sopenharmony_ci#include <linux/videodev2.h> 178c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 188c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("i2c device driver for cs53l32a Audio ADC"); 218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Martin Vaughan"); 228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic bool debug; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cimodule_param(debug, bool, 0644); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On"); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistruct cs53l32a_state { 328c2ecf20Sopenharmony_ci struct v4l2_subdev sd; 338c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler hdl; 348c2ecf20Sopenharmony_ci}; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic inline struct cs53l32a_state *to_state(struct v4l2_subdev *sd) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci return container_of(sd, struct cs53l32a_state, sd); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci return &container_of(ctrl->handler, struct cs53l32a_state, hdl)->sd; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */ 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic int cs53l32a_write(struct v4l2_subdev *sd, u8 reg, u8 value) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(sd); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci return i2c_smbus_write_byte_data(client, reg, value); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic int cs53l32a_read(struct v4l2_subdev *sd, u8 reg) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(sd); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci return i2c_smbus_read_byte_data(client, reg); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cistatic int cs53l32a_s_routing(struct v4l2_subdev *sd, 638c2ecf20Sopenharmony_ci u32 input, u32 output, u32 config) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci /* There are 2 physical inputs, but the second input can be 668c2ecf20Sopenharmony_ci placed in two modes, the first mode bypasses the PGA (gain), 678c2ecf20Sopenharmony_ci the second goes through the PGA. Hence there are three 688c2ecf20Sopenharmony_ci possible inputs to choose from. */ 698c2ecf20Sopenharmony_ci if (input > 2) { 708c2ecf20Sopenharmony_ci v4l2_err(sd, "Invalid input %d.\n", input); 718c2ecf20Sopenharmony_ci return -EINVAL; 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x01, 0x01 + (input << 4)); 748c2ecf20Sopenharmony_ci return 0; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic int cs53l32a_s_ctrl(struct v4l2_ctrl *ctrl) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci struct v4l2_subdev *sd = to_sd(ctrl); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci switch (ctrl->id) { 828c2ecf20Sopenharmony_ci case V4L2_CID_AUDIO_MUTE: 838c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x03, ctrl->val ? 0xf0 : 0x30); 848c2ecf20Sopenharmony_ci return 0; 858c2ecf20Sopenharmony_ci case V4L2_CID_AUDIO_VOLUME: 868c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x04, (u8)ctrl->val); 878c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x05, (u8)ctrl->val); 888c2ecf20Sopenharmony_ci return 0; 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci return -EINVAL; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic int cs53l32a_log_status(struct v4l2_subdev *sd) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci struct cs53l32a_state *state = to_state(sd); 968c2ecf20Sopenharmony_ci u8 v = cs53l32a_read(sd, 0x01); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci v4l2_info(sd, "Input: %d\n", (v >> 4) & 3); 998c2ecf20Sopenharmony_ci v4l2_ctrl_handler_log_status(&state->hdl, sd->name); 1008c2ecf20Sopenharmony_ci return 0; 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */ 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops cs53l32a_ctrl_ops = { 1068c2ecf20Sopenharmony_ci .s_ctrl = cs53l32a_s_ctrl, 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops cs53l32a_core_ops = { 1108c2ecf20Sopenharmony_ci .log_status = cs53l32a_log_status, 1118c2ecf20Sopenharmony_ci}; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_audio_ops cs53l32a_audio_ops = { 1148c2ecf20Sopenharmony_ci .s_routing = cs53l32a_s_routing, 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops cs53l32a_ops = { 1188c2ecf20Sopenharmony_ci .core = &cs53l32a_core_ops, 1198c2ecf20Sopenharmony_ci .audio = &cs53l32a_audio_ops, 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */ 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* i2c implementation */ 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* 1278c2ecf20Sopenharmony_ci * Generic i2c probe 1288c2ecf20Sopenharmony_ci * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic int cs53l32a_probe(struct i2c_client *client, 1328c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct cs53l32a_state *state; 1358c2ecf20Sopenharmony_ci struct v4l2_subdev *sd; 1368c2ecf20Sopenharmony_ci int i; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* Check if the adapter supports the needed features */ 1398c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1408c2ecf20Sopenharmony_ci return -EIO; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (!id) 1438c2ecf20Sopenharmony_ci strscpy(client->name, "cs53l32a", sizeof(client->name)); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci v4l_info(client, "chip found @ 0x%x (%s)\n", 1468c2ecf20Sopenharmony_ci client->addr << 1, client->adapter->name); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); 1498c2ecf20Sopenharmony_ci if (state == NULL) 1508c2ecf20Sopenharmony_ci return -ENOMEM; 1518c2ecf20Sopenharmony_ci sd = &state->sd; 1528c2ecf20Sopenharmony_ci v4l2_i2c_subdev_init(sd, client, &cs53l32a_ops); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci for (i = 1; i <= 7; i++) { 1558c2ecf20Sopenharmony_ci u8 v = cs53l32a_read(sd, i); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v); 1588c2ecf20Sopenharmony_ci } 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci v4l2_ctrl_handler_init(&state->hdl, 2); 1618c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&state->hdl, &cs53l32a_ctrl_ops, 1628c2ecf20Sopenharmony_ci V4L2_CID_AUDIO_VOLUME, -96, 12, 1, 0); 1638c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&state->hdl, &cs53l32a_ctrl_ops, 1648c2ecf20Sopenharmony_ci V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0); 1658c2ecf20Sopenharmony_ci sd->ctrl_handler = &state->hdl; 1668c2ecf20Sopenharmony_ci if (state->hdl.error) { 1678c2ecf20Sopenharmony_ci int err = state->hdl.error; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci v4l2_ctrl_handler_free(&state->hdl); 1708c2ecf20Sopenharmony_ci return err; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci /* Set cs53l32a internal register for Adaptec 2010/2410 setup */ 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x01, 0x21); 1768c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x02, 0x29); 1778c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x03, 0x30); 1788c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x04, 0x00); 1798c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x05, 0x00); 1808c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x06, 0x00); 1818c2ecf20Sopenharmony_ci cs53l32a_write(sd, 0x07, 0x00); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* Display results, should be 0x21,0x29,0x30,0x00,0x00,0x00,0x00 */ 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci for (i = 1; i <= 7; i++) { 1868c2ecf20Sopenharmony_ci u8 v = cs53l32a_read(sd, i); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, "Read Reg %d %02x\n", i, v); 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci return 0; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic int cs53l32a_remove(struct i2c_client *client) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci struct v4l2_subdev *sd = i2c_get_clientdata(client); 1968c2ecf20Sopenharmony_ci struct cs53l32a_state *state = to_state(sd); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci v4l2_device_unregister_subdev(sd); 1998c2ecf20Sopenharmony_ci v4l2_ctrl_handler_free(&state->hdl); 2008c2ecf20Sopenharmony_ci return 0; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic const struct i2c_device_id cs53l32a_id[] = { 2048c2ecf20Sopenharmony_ci { "cs53l32a", 0 }, 2058c2ecf20Sopenharmony_ci { } 2068c2ecf20Sopenharmony_ci}; 2078c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, cs53l32a_id); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic struct i2c_driver cs53l32a_driver = { 2108c2ecf20Sopenharmony_ci .driver = { 2118c2ecf20Sopenharmony_ci .name = "cs53l32a", 2128c2ecf20Sopenharmony_ci }, 2138c2ecf20Sopenharmony_ci .probe = cs53l32a_probe, 2148c2ecf20Sopenharmony_ci .remove = cs53l32a_remove, 2158c2ecf20Sopenharmony_ci .id_table = cs53l32a_id, 2168c2ecf20Sopenharmony_ci}; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cimodule_i2c_driver(cs53l32a_driver); 219