Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 1 | /* |
| 2 | * m52790 i2c ivtv driver. |
| 3 | * Copyright (C) 2007 Hans Verkuil |
| 4 | * |
| 5 | * A/V source switching Mitsubishi M52790SP/FP |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 26 | #include <linux/ioctl.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | #include <linux/i2c.h> |
Hans Verkuil | 33b687c | 2008-07-25 05:32:50 -0300 | [diff] [blame] | 29 | #include <linux/videodev2.h> |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 30 | #include <media/m52790.h> |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 31 | #include <media/v4l2-device.h> |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 32 | #include <media/v4l2-chip-ident.h> |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 33 | |
| 34 | MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch"); |
| 35 | MODULE_AUTHOR("Hans Verkuil"); |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 38 | |
| 39 | struct m52790_state { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 40 | struct v4l2_subdev sd; |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 41 | u16 input; |
| 42 | u16 output; |
| 43 | }; |
| 44 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 45 | static inline struct m52790_state *to_state(struct v4l2_subdev *sd) |
| 46 | { |
| 47 | return container_of(sd, struct m52790_state, sd); |
| 48 | } |
| 49 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 50 | /* ----------------------------------------------------------------------- */ |
| 51 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 52 | static int m52790_write(struct v4l2_subdev *sd) |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 53 | { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 54 | struct m52790_state *state = to_state(sd); |
| 55 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 56 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 57 | u8 sw1 = (state->input | state->output) & 0xff; |
| 58 | u8 sw2 = (state->input | state->output) >> 8; |
| 59 | |
| 60 | return i2c_smbus_write_byte_data(client, sw1, sw2); |
| 61 | } |
| 62 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 63 | /* Note: audio and video are linked and cannot be switched separately. |
| 64 | So audio and video routing commands are identical for this chip. |
| 65 | In theory the video amplifier and audio modes could be handled |
| 66 | separately for the output, but that seems to be overkill right now. |
| 67 | The same holds for implementing an audio mute control, this is now |
| 68 | part of the audio output routing. The normal case is that another |
| 69 | chip takes care of the actual muting so making it part of the |
| 70 | output routing seems to be the right thing to do for now. */ |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 71 | static int m52790_s_routing(struct v4l2_subdev *sd, |
| 72 | u32 input, u32 output, u32 config) |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 73 | { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 74 | struct m52790_state *state = to_state(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 75 | |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 76 | state->input = input; |
| 77 | state->output = output; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 78 | m52790_write(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 82 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 83 | static int m52790_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 84 | { |
| 85 | struct m52790_state *state = to_state(sd); |
| 86 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 87 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 88 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 89 | return -EINVAL; |
| 90 | if (!capable(CAP_SYS_ADMIN)) |
| 91 | return -EPERM; |
| 92 | if (reg->reg != 0) |
| 93 | return -EINVAL; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 94 | reg->size = 1; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 95 | reg->val = state->input | state->output; |
| 96 | return 0; |
| 97 | } |
| 98 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 99 | static int m52790_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 100 | { |
| 101 | struct m52790_state *state = to_state(sd); |
| 102 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 103 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 104 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 105 | return -EINVAL; |
| 106 | if (!capable(CAP_SYS_ADMIN)) |
| 107 | return -EPERM; |
| 108 | if (reg->reg != 0) |
| 109 | return -EINVAL; |
| 110 | state->input = reg->val & 0x0303; |
| 111 | state->output = reg->val & ~0x0303; |
| 112 | m52790_write(sd); |
| 113 | return 0; |
| 114 | } |
| 115 | #endif |
| 116 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 117 | static int m52790_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 118 | { |
| 119 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 120 | |
| 121 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_M52790, 0); |
| 122 | } |
| 123 | |
| 124 | static int m52790_log_status(struct v4l2_subdev *sd) |
| 125 | { |
| 126 | struct m52790_state *state = to_state(sd); |
| 127 | |
| 128 | v4l2_info(sd, "Switch 1: %02x\n", |
| 129 | (state->input | state->output) & 0xff); |
| 130 | v4l2_info(sd, "Switch 2: %02x\n", |
| 131 | (state->input | state->output) >> 8); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 135 | /* ----------------------------------------------------------------------- */ |
| 136 | |
| 137 | static const struct v4l2_subdev_core_ops m52790_core_ops = { |
| 138 | .log_status = m52790_log_status, |
| 139 | .g_chip_ident = m52790_g_chip_ident, |
| 140 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 141 | .g_register = m52790_g_register, |
| 142 | .s_register = m52790_s_register, |
| 143 | #endif |
| 144 | }; |
| 145 | |
| 146 | static const struct v4l2_subdev_audio_ops m52790_audio_ops = { |
| 147 | .s_routing = m52790_s_routing, |
| 148 | }; |
| 149 | |
| 150 | static const struct v4l2_subdev_video_ops m52790_video_ops = { |
| 151 | .s_routing = m52790_s_routing, |
| 152 | }; |
| 153 | |
| 154 | static const struct v4l2_subdev_ops m52790_ops = { |
| 155 | .core = &m52790_core_ops, |
| 156 | .audio = &m52790_audio_ops, |
| 157 | .video = &m52790_video_ops, |
| 158 | }; |
| 159 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 160 | /* ----------------------------------------------------------------------- */ |
| 161 | |
| 162 | /* i2c implementation */ |
| 163 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 164 | static int m52790_probe(struct i2c_client *client, |
| 165 | const struct i2c_device_id *id) |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 166 | { |
| 167 | struct m52790_state *state; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 168 | struct v4l2_subdev *sd; |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 169 | |
| 170 | /* Check if the adapter supports the needed features */ |
| 171 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 172 | return -EIO; |
| 173 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 174 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 175 | client->addr << 1, client->adapter->name); |
| 176 | |
Herton Ronaldo Krzesinski | 80845a3 | 2011-04-01 14:12:02 -0300 | [diff] [blame] | 177 | state = kzalloc(sizeof(struct m52790_state), GFP_KERNEL); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 178 | if (state == NULL) |
| 179 | return -ENOMEM; |
| 180 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 181 | sd = &state->sd; |
| 182 | v4l2_i2c_subdev_init(sd, client, &m52790_ops); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 183 | state->input = M52790_IN_TUNER; |
| 184 | state->output = M52790_OUT_STEREO; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 185 | m52790_write(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int m52790_remove(struct i2c_client *client) |
| 190 | { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 191 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 192 | |
| 193 | v4l2_device_unregister_subdev(sd); |
| 194 | kfree(to_state(sd)); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* ----------------------------------------------------------------------- */ |
| 199 | |
Jean Delvare | af29486 | 2008-05-18 20:49:40 +0200 | [diff] [blame] | 200 | static const struct i2c_device_id m52790_id[] = { |
| 201 | { "m52790", 0 }, |
| 202 | { } |
| 203 | }; |
| 204 | MODULE_DEVICE_TABLE(i2c, m52790_id); |
| 205 | |
Hans Verkuil | d30f60b | 2010-09-15 15:18:29 -0300 | [diff] [blame] | 206 | static struct i2c_driver m52790_driver = { |
| 207 | .driver = { |
| 208 | .owner = THIS_MODULE, |
| 209 | .name = "m52790", |
| 210 | }, |
| 211 | .probe = m52790_probe, |
| 212 | .remove = m52790_remove, |
| 213 | .id_table = m52790_id, |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 214 | }; |
Hans Verkuil | d30f60b | 2010-09-15 15:18:29 -0300 | [diff] [blame] | 215 | |
| 216 | static __init int init_m52790(void) |
| 217 | { |
| 218 | return i2c_add_driver(&m52790_driver); |
| 219 | } |
| 220 | |
| 221 | static __exit void exit_m52790(void) |
| 222 | { |
| 223 | i2c_del_driver(&m52790_driver); |
| 224 | } |
| 225 | |
| 226 | module_init(init_m52790); |
| 227 | module_exit(exit_m52790); |