1cb7a01acSMauro Carvalho Chehab /* 2cb7a01acSMauro Carvalho Chehab * vp27smpx - driver version 0.0.1 3cb7a01acSMauro Carvalho Chehab * 4cb7a01acSMauro Carvalho Chehab * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> 5cb7a01acSMauro Carvalho Chehab * 6cb7a01acSMauro Carvalho Chehab * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com> 7cb7a01acSMauro Carvalho Chehab * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp> 8cb7a01acSMauro Carvalho Chehab * 9cb7a01acSMauro Carvalho Chehab * This program is free software; you can redistribute it and/or modify 10cb7a01acSMauro Carvalho Chehab * it under the terms of the GNU General Public License as published by 11cb7a01acSMauro Carvalho Chehab * the Free Software Foundation; either version 2 of the License, or 12cb7a01acSMauro Carvalho Chehab * (at your option) any later version. 13cb7a01acSMauro Carvalho Chehab * 14cb7a01acSMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 15cb7a01acSMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 16cb7a01acSMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17cb7a01acSMauro Carvalho Chehab * GNU General Public License for more details. 18cb7a01acSMauro Carvalho Chehab * 19cb7a01acSMauro Carvalho Chehab * You should have received a copy of the GNU General Public License 20cb7a01acSMauro Carvalho Chehab * along with this program; if not, write to the Free Software 21cb7a01acSMauro Carvalho Chehab * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22cb7a01acSMauro Carvalho Chehab */ 23cb7a01acSMauro Carvalho Chehab 24cb7a01acSMauro Carvalho Chehab #include <linux/module.h> 25cb7a01acSMauro Carvalho Chehab #include <linux/types.h> 26cb7a01acSMauro Carvalho Chehab #include <linux/slab.h> 27cb7a01acSMauro Carvalho Chehab #include <linux/ioctl.h> 28cb7a01acSMauro Carvalho Chehab #include <asm/uaccess.h> 29cb7a01acSMauro Carvalho Chehab #include <linux/i2c.h> 30cb7a01acSMauro Carvalho Chehab #include <linux/videodev2.h> 31cb7a01acSMauro Carvalho Chehab #include <media/v4l2-device.h> 32cb7a01acSMauro Carvalho Chehab #include <media/v4l2-chip-ident.h> 33cb7a01acSMauro Carvalho Chehab 34cb7a01acSMauro Carvalho Chehab MODULE_DESCRIPTION("vp27smpx driver"); 35cb7a01acSMauro Carvalho Chehab MODULE_AUTHOR("Hans Verkuil"); 36cb7a01acSMauro Carvalho Chehab MODULE_LICENSE("GPL"); 37cb7a01acSMauro Carvalho Chehab 38cb7a01acSMauro Carvalho Chehab 39cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */ 40cb7a01acSMauro Carvalho Chehab 41cb7a01acSMauro Carvalho Chehab struct vp27smpx_state { 42cb7a01acSMauro Carvalho Chehab struct v4l2_subdev sd; 43cb7a01acSMauro Carvalho Chehab int radio; 44cb7a01acSMauro Carvalho Chehab u32 audmode; 45cb7a01acSMauro Carvalho Chehab }; 46cb7a01acSMauro Carvalho Chehab 47cb7a01acSMauro Carvalho Chehab static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd) 48cb7a01acSMauro Carvalho Chehab { 49cb7a01acSMauro Carvalho Chehab return container_of(sd, struct vp27smpx_state, sd); 50cb7a01acSMauro Carvalho Chehab } 51cb7a01acSMauro Carvalho Chehab 52cb7a01acSMauro Carvalho Chehab static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode) 53cb7a01acSMauro Carvalho Chehab { 54cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state = to_state(sd); 55cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd); 56cb7a01acSMauro Carvalho Chehab u8 data[3] = { 0x00, 0x00, 0x04 }; 57cb7a01acSMauro Carvalho Chehab 58cb7a01acSMauro Carvalho Chehab switch (audmode) { 59cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_MONO: 60cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_LANG1: 61cb7a01acSMauro Carvalho Chehab break; 62cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_STEREO: 63cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_LANG1_LANG2: 64cb7a01acSMauro Carvalho Chehab data[1] = 0x01; 65cb7a01acSMauro Carvalho Chehab break; 66cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_LANG2: 67cb7a01acSMauro Carvalho Chehab data[1] = 0x02; 68cb7a01acSMauro Carvalho Chehab break; 69cb7a01acSMauro Carvalho Chehab } 70cb7a01acSMauro Carvalho Chehab 71cb7a01acSMauro Carvalho Chehab if (i2c_master_send(client, data, sizeof(data)) != sizeof(data)) 72cb7a01acSMauro Carvalho Chehab v4l2_err(sd, "I/O error setting audmode\n"); 73cb7a01acSMauro Carvalho Chehab else 74cb7a01acSMauro Carvalho Chehab state->audmode = audmode; 75cb7a01acSMauro Carvalho Chehab } 76cb7a01acSMauro Carvalho Chehab 77cb7a01acSMauro Carvalho Chehab static int vp27smpx_s_radio(struct v4l2_subdev *sd) 78cb7a01acSMauro Carvalho Chehab { 79cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state = to_state(sd); 80cb7a01acSMauro Carvalho Chehab 81cb7a01acSMauro Carvalho Chehab state->radio = 1; 82cb7a01acSMauro Carvalho Chehab return 0; 83cb7a01acSMauro Carvalho Chehab } 84cb7a01acSMauro Carvalho Chehab 85cb7a01acSMauro Carvalho Chehab static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm) 86cb7a01acSMauro Carvalho Chehab { 87cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state = to_state(sd); 88cb7a01acSMauro Carvalho Chehab 89cb7a01acSMauro Carvalho Chehab state->radio = 0; 90cb7a01acSMauro Carvalho Chehab return 0; 91cb7a01acSMauro Carvalho Chehab } 92cb7a01acSMauro Carvalho Chehab 932f73c7c5SHans Verkuil static int vp27smpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt) 94cb7a01acSMauro Carvalho Chehab { 95cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state = to_state(sd); 96cb7a01acSMauro Carvalho Chehab 97cb7a01acSMauro Carvalho Chehab if (!state->radio) 98cb7a01acSMauro Carvalho Chehab vp27smpx_set_audmode(sd, vt->audmode); 99cb7a01acSMauro Carvalho Chehab return 0; 100cb7a01acSMauro Carvalho Chehab } 101cb7a01acSMauro Carvalho Chehab 102cb7a01acSMauro Carvalho Chehab static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) 103cb7a01acSMauro Carvalho Chehab { 104cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state = to_state(sd); 105cb7a01acSMauro Carvalho Chehab 106cb7a01acSMauro Carvalho Chehab if (state->radio) 107cb7a01acSMauro Carvalho Chehab return 0; 108cb7a01acSMauro Carvalho Chehab vt->audmode = state->audmode; 109cb7a01acSMauro Carvalho Chehab vt->capability = V4L2_TUNER_CAP_STEREO | 110cb7a01acSMauro Carvalho Chehab V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2; 111cb7a01acSMauro Carvalho Chehab vt->rxsubchans = V4L2_TUNER_SUB_MONO; 112cb7a01acSMauro Carvalho Chehab return 0; 113cb7a01acSMauro Carvalho Chehab } 114cb7a01acSMauro Carvalho Chehab 115cb7a01acSMauro Carvalho Chehab static int vp27smpx_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) 116cb7a01acSMauro Carvalho Chehab { 117cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd); 118cb7a01acSMauro Carvalho Chehab 119cb7a01acSMauro Carvalho Chehab return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_VP27SMPX, 0); 120cb7a01acSMauro Carvalho Chehab } 121cb7a01acSMauro Carvalho Chehab 122cb7a01acSMauro Carvalho Chehab static int vp27smpx_log_status(struct v4l2_subdev *sd) 123cb7a01acSMauro Carvalho Chehab { 124cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state = to_state(sd); 125cb7a01acSMauro Carvalho Chehab 126cb7a01acSMauro Carvalho Chehab v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode, 127cb7a01acSMauro Carvalho Chehab state->radio ? " (Radio)" : ""); 128cb7a01acSMauro Carvalho Chehab return 0; 129cb7a01acSMauro Carvalho Chehab } 130cb7a01acSMauro Carvalho Chehab 131cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */ 132cb7a01acSMauro Carvalho Chehab 133cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_core_ops vp27smpx_core_ops = { 134cb7a01acSMauro Carvalho Chehab .log_status = vp27smpx_log_status, 135cb7a01acSMauro Carvalho Chehab .g_chip_ident = vp27smpx_g_chip_ident, 136cb7a01acSMauro Carvalho Chehab .s_std = vp27smpx_s_std, 137cb7a01acSMauro Carvalho Chehab }; 138cb7a01acSMauro Carvalho Chehab 139cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = { 140cb7a01acSMauro Carvalho Chehab .s_radio = vp27smpx_s_radio, 141cb7a01acSMauro Carvalho Chehab .s_tuner = vp27smpx_s_tuner, 142cb7a01acSMauro Carvalho Chehab .g_tuner = vp27smpx_g_tuner, 143cb7a01acSMauro Carvalho Chehab }; 144cb7a01acSMauro Carvalho Chehab 145cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_ops vp27smpx_ops = { 146cb7a01acSMauro Carvalho Chehab .core = &vp27smpx_core_ops, 147cb7a01acSMauro Carvalho Chehab .tuner = &vp27smpx_tuner_ops, 148cb7a01acSMauro Carvalho Chehab }; 149cb7a01acSMauro Carvalho Chehab 150cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */ 151cb7a01acSMauro Carvalho Chehab 152cb7a01acSMauro Carvalho Chehab /* i2c implementation */ 153cb7a01acSMauro Carvalho Chehab 154cb7a01acSMauro Carvalho Chehab /* 155cb7a01acSMauro Carvalho Chehab * Generic i2c probe 156cb7a01acSMauro Carvalho Chehab * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1' 157cb7a01acSMauro Carvalho Chehab */ 158cb7a01acSMauro Carvalho Chehab 159cb7a01acSMauro Carvalho Chehab static int vp27smpx_probe(struct i2c_client *client, 160cb7a01acSMauro Carvalho Chehab const struct i2c_device_id *id) 161cb7a01acSMauro Carvalho Chehab { 162cb7a01acSMauro Carvalho Chehab struct vp27smpx_state *state; 163cb7a01acSMauro Carvalho Chehab struct v4l2_subdev *sd; 164cb7a01acSMauro Carvalho Chehab 165cb7a01acSMauro Carvalho Chehab /* Check if the adapter supports the needed features */ 166cb7a01acSMauro Carvalho Chehab if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 167cb7a01acSMauro Carvalho Chehab return -EIO; 168cb7a01acSMauro Carvalho Chehab 169cb7a01acSMauro Carvalho Chehab v4l_info(client, "chip found @ 0x%x (%s)\n", 170cb7a01acSMauro Carvalho Chehab client->addr << 1, client->adapter->name); 171cb7a01acSMauro Carvalho Chehab 172*c02b211dSLaurent Pinchart state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); 173cb7a01acSMauro Carvalho Chehab if (state == NULL) 174cb7a01acSMauro Carvalho Chehab return -ENOMEM; 175cb7a01acSMauro Carvalho Chehab sd = &state->sd; 176cb7a01acSMauro Carvalho Chehab v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops); 177cb7a01acSMauro Carvalho Chehab state->audmode = V4L2_TUNER_MODE_STEREO; 178cb7a01acSMauro Carvalho Chehab 179cb7a01acSMauro Carvalho Chehab /* initialize vp27smpx */ 180cb7a01acSMauro Carvalho Chehab vp27smpx_set_audmode(sd, state->audmode); 181cb7a01acSMauro Carvalho Chehab return 0; 182cb7a01acSMauro Carvalho Chehab } 183cb7a01acSMauro Carvalho Chehab 184cb7a01acSMauro Carvalho Chehab static int vp27smpx_remove(struct i2c_client *client) 185cb7a01acSMauro Carvalho Chehab { 186cb7a01acSMauro Carvalho Chehab struct v4l2_subdev *sd = i2c_get_clientdata(client); 187cb7a01acSMauro Carvalho Chehab 188cb7a01acSMauro Carvalho Chehab v4l2_device_unregister_subdev(sd); 189cb7a01acSMauro Carvalho Chehab return 0; 190cb7a01acSMauro Carvalho Chehab } 191cb7a01acSMauro Carvalho Chehab 192cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */ 193cb7a01acSMauro Carvalho Chehab 194cb7a01acSMauro Carvalho Chehab static const struct i2c_device_id vp27smpx_id[] = { 195cb7a01acSMauro Carvalho Chehab { "vp27smpx", 0 }, 196cb7a01acSMauro Carvalho Chehab { } 197cb7a01acSMauro Carvalho Chehab }; 198cb7a01acSMauro Carvalho Chehab MODULE_DEVICE_TABLE(i2c, vp27smpx_id); 199cb7a01acSMauro Carvalho Chehab 200cb7a01acSMauro Carvalho Chehab static struct i2c_driver vp27smpx_driver = { 201cb7a01acSMauro Carvalho Chehab .driver = { 202cb7a01acSMauro Carvalho Chehab .owner = THIS_MODULE, 203cb7a01acSMauro Carvalho Chehab .name = "vp27smpx", 204cb7a01acSMauro Carvalho Chehab }, 205cb7a01acSMauro Carvalho Chehab .probe = vp27smpx_probe, 206cb7a01acSMauro Carvalho Chehab .remove = vp27smpx_remove, 207cb7a01acSMauro Carvalho Chehab .id_table = vp27smpx_id, 208cb7a01acSMauro Carvalho Chehab }; 209cb7a01acSMauro Carvalho Chehab 210cb7a01acSMauro Carvalho Chehab module_i2c_driver(vp27smpx_driver); 211