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