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> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 28 | #include <linux/i2c.h> |
Hans Verkuil | 33b687c | 2008-07-25 05:32:50 -0300 | [diff] [blame] | 29 | #include <linux/videodev2.h> |
Mauro Carvalho Chehab | b5dcee2 | 2015-11-10 12:01:44 -0200 | [diff] [blame] | 30 | #include <media/i2c/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 | |
| 33 | MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch"); |
| 34 | MODULE_AUTHOR("Hans Verkuil"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 37 | |
| 38 | struct m52790_state { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 39 | struct v4l2_subdev sd; |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 40 | u16 input; |
| 41 | u16 output; |
| 42 | }; |
| 43 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 44 | static inline struct m52790_state *to_state(struct v4l2_subdev *sd) |
| 45 | { |
| 46 | return container_of(sd, struct m52790_state, sd); |
| 47 | } |
| 48 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 49 | /* ----------------------------------------------------------------------- */ |
| 50 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 51 | static int m52790_write(struct v4l2_subdev *sd) |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 52 | { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 53 | struct m52790_state *state = to_state(sd); |
| 54 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 55 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 56 | u8 sw1 = (state->input | state->output) & 0xff; |
| 57 | u8 sw2 = (state->input | state->output) >> 8; |
| 58 | |
| 59 | return i2c_smbus_write_byte_data(client, sw1, sw2); |
| 60 | } |
| 61 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 62 | /* Note: audio and video are linked and cannot be switched separately. |
| 63 | So audio and video routing commands are identical for this chip. |
| 64 | In theory the video amplifier and audio modes could be handled |
| 65 | separately for the output, but that seems to be overkill right now. |
| 66 | The same holds for implementing an audio mute control, this is now |
| 67 | part of the audio output routing. The normal case is that another |
| 68 | chip takes care of the actual muting so making it part of the |
| 69 | output routing seems to be the right thing to do for now. */ |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 70 | static int m52790_s_routing(struct v4l2_subdev *sd, |
| 71 | u32 input, u32 output, u32 config) |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 72 | { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 73 | struct m52790_state *state = to_state(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 74 | |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 75 | state->input = input; |
| 76 | state->output = output; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 77 | m52790_write(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 81 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | struct m52790_state *state = to_state(sd); |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 85 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 86 | if (reg->reg != 0) |
| 87 | return -EINVAL; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 88 | reg->size = 1; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 89 | reg->val = state->input | state->output; |
| 90 | return 0; |
| 91 | } |
| 92 | |
Hans Verkuil | 977ba3b | 2013-03-24 08:28:46 -0300 | [diff] [blame] | 93 | static int m52790_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 94 | { |
| 95 | struct m52790_state *state = to_state(sd); |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 96 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 97 | if (reg->reg != 0) |
| 98 | return -EINVAL; |
| 99 | state->input = reg->val & 0x0303; |
| 100 | state->output = reg->val & ~0x0303; |
| 101 | m52790_write(sd); |
| 102 | return 0; |
| 103 | } |
| 104 | #endif |
| 105 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 106 | static int m52790_log_status(struct v4l2_subdev *sd) |
| 107 | { |
| 108 | struct m52790_state *state = to_state(sd); |
| 109 | |
| 110 | v4l2_info(sd, "Switch 1: %02x\n", |
| 111 | (state->input | state->output) & 0xff); |
| 112 | v4l2_info(sd, "Switch 2: %02x\n", |
| 113 | (state->input | state->output) >> 8); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 117 | /* ----------------------------------------------------------------------- */ |
| 118 | |
| 119 | static const struct v4l2_subdev_core_ops m52790_core_ops = { |
| 120 | .log_status = m52790_log_status, |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 121 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 122 | .g_register = m52790_g_register, |
| 123 | .s_register = m52790_s_register, |
| 124 | #endif |
| 125 | }; |
| 126 | |
| 127 | static const struct v4l2_subdev_audio_ops m52790_audio_ops = { |
| 128 | .s_routing = m52790_s_routing, |
| 129 | }; |
| 130 | |
| 131 | static const struct v4l2_subdev_video_ops m52790_video_ops = { |
| 132 | .s_routing = m52790_s_routing, |
| 133 | }; |
| 134 | |
| 135 | static const struct v4l2_subdev_ops m52790_ops = { |
| 136 | .core = &m52790_core_ops, |
| 137 | .audio = &m52790_audio_ops, |
| 138 | .video = &m52790_video_ops, |
| 139 | }; |
| 140 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 141 | /* ----------------------------------------------------------------------- */ |
| 142 | |
| 143 | /* i2c implementation */ |
| 144 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 145 | static int m52790_probe(struct i2c_client *client, |
| 146 | const struct i2c_device_id *id) |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 147 | { |
| 148 | struct m52790_state *state; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 149 | struct v4l2_subdev *sd; |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 150 | |
| 151 | /* Check if the adapter supports the needed features */ |
| 152 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 153 | return -EIO; |
| 154 | |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 155 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 156 | client->addr << 1, client->adapter->name); |
| 157 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 158 | state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 159 | if (state == NULL) |
| 160 | return -ENOMEM; |
| 161 | |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 162 | sd = &state->sd; |
| 163 | v4l2_i2c_subdev_init(sd, client, &m52790_ops); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 164 | state->input = M52790_IN_TUNER; |
| 165 | state->output = M52790_OUT_STEREO; |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 166 | m52790_write(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int m52790_remove(struct i2c_client *client) |
| 171 | { |
Hans Verkuil | 5d302f2e | 2008-11-29 12:51:32 -0300 | [diff] [blame] | 172 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 173 | |
| 174 | v4l2_device_unregister_subdev(sd); |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* ----------------------------------------------------------------------- */ |
| 179 | |
Jean Delvare | af29486 | 2008-05-18 20:49:40 +0200 | [diff] [blame] | 180 | static const struct i2c_device_id m52790_id[] = { |
| 181 | { "m52790", 0 }, |
| 182 | { } |
| 183 | }; |
| 184 | MODULE_DEVICE_TABLE(i2c, m52790_id); |
| 185 | |
Hans Verkuil | d30f60b | 2010-09-15 15:18:29 -0300 | [diff] [blame] | 186 | static struct i2c_driver m52790_driver = { |
| 187 | .driver = { |
Hans Verkuil | d30f60b | 2010-09-15 15:18:29 -0300 | [diff] [blame] | 188 | .name = "m52790", |
| 189 | }, |
| 190 | .probe = m52790_probe, |
| 191 | .remove = m52790_remove, |
| 192 | .id_table = m52790_id, |
Hans Verkuil | 761dacd | 2007-10-30 05:41:25 -0300 | [diff] [blame] | 193 | }; |
Hans Verkuil | d30f60b | 2010-09-15 15:18:29 -0300 | [diff] [blame] | 194 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 195 | module_i2c_driver(m52790_driver); |