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