blob: a7a8f9a4e45cc52d79c321a2f79dca4d66c70a66 [file] [log] [blame]
Hans Verkuil761dacd2007-10-30 05:41:25 -03001/*
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 Verkuil761dacd2007-10-30 05:41:25 -030016 */
17
18
19#include <linux/module.h>
20#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030022#include <linux/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030024#include <linux/i2c.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030025#include <linux/videodev2.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020026#include <media/i2c/m52790.h>
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030027#include <media/v4l2-device.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030028
29MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch");
30MODULE_AUTHOR("Hans Verkuil");
31MODULE_LICENSE("GPL");
32
Hans Verkuil761dacd2007-10-30 05:41:25 -030033
34struct m52790_state {
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030035 struct v4l2_subdev sd;
Hans Verkuil761dacd2007-10-30 05:41:25 -030036 u16 input;
37 u16 output;
38};
39
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030040static inline struct m52790_state *to_state(struct v4l2_subdev *sd)
41{
42 return container_of(sd, struct m52790_state, sd);
43}
44
Hans Verkuil761dacd2007-10-30 05:41:25 -030045/* ----------------------------------------------------------------------- */
46
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030047static int m52790_write(struct v4l2_subdev *sd)
Hans Verkuil761dacd2007-10-30 05:41:25 -030048{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030049 struct m52790_state *state = to_state(sd);
50 struct i2c_client *client = v4l2_get_subdevdata(sd);
51
Hans Verkuil761dacd2007-10-30 05:41:25 -030052 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 Verkuil5d302f2e2008-11-29 12:51:32 -030058/* 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 Verkuil5325b422009-04-02 11:26:22 -030066static int m52790_s_routing(struct v4l2_subdev *sd,
67 u32 input, u32 output, u32 config)
Hans Verkuil761dacd2007-10-30 05:41:25 -030068{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030069 struct m52790_state *state = to_state(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -030070
Hans Verkuil5325b422009-04-02 11:26:22 -030071 state->input = input;
72 state->output = output;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030073 m52790_write(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -030074 return 0;
75}
76
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030077#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -030078static int m52790_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030079{
80 struct m52790_state *state = to_state(sd);
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030081
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030082 if (reg->reg != 0)
83 return -EINVAL;
Hans Verkuilaecde8b52008-12-30 07:14:19 -030084 reg->size = 1;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030085 reg->val = state->input | state->output;
86 return 0;
87}
88
Hans Verkuil977ba3b2013-03-24 08:28:46 -030089static int m52790_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030090{
91 struct m52790_state *state = to_state(sd);
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030092
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030093 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 Verkuil5d302f2e2008-11-29 12:51:32 -0300102static 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 Verkuil5d302f2e2008-11-29 12:51:32 -0300113/* ----------------------------------------------------------------------- */
114
115static const struct v4l2_subdev_core_ops m52790_core_ops = {
116 .log_status = m52790_log_status,
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300117#ifdef CONFIG_VIDEO_ADV_DEBUG
118 .g_register = m52790_g_register,
119 .s_register = m52790_s_register,
120#endif
121};
122
123static const struct v4l2_subdev_audio_ops m52790_audio_ops = {
124 .s_routing = m52790_s_routing,
125};
126
127static const struct v4l2_subdev_video_ops m52790_video_ops = {
128 .s_routing = m52790_s_routing,
129};
130
131static 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 Verkuil761dacd2007-10-30 05:41:25 -0300137/* ----------------------------------------------------------------------- */
138
139/* i2c implementation */
140
Jean Delvared2653e92008-04-29 23:11:39 +0200141static int m52790_probe(struct i2c_client *client,
142 const struct i2c_device_id *id)
Hans Verkuil761dacd2007-10-30 05:41:25 -0300143{
144 struct m52790_state *state;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300145 struct v4l2_subdev *sd;
Hans Verkuil761dacd2007-10-30 05:41:25 -0300146
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 Verkuil761dacd2007-10-30 05:41:25 -0300151 v4l_info(client, "chip found @ 0x%x (%s)\n",
152 client->addr << 1, client->adapter->name);
153
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300154 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300155 if (state == NULL)
156 return -ENOMEM;
157
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300158 sd = &state->sd;
159 v4l2_i2c_subdev_init(sd, client, &m52790_ops);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300160 state->input = M52790_IN_TUNER;
161 state->output = M52790_OUT_STEREO;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300162 m52790_write(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300163 return 0;
164}
165
166static int m52790_remove(struct i2c_client *client)
167{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300168 struct v4l2_subdev *sd = i2c_get_clientdata(client);
169
170 v4l2_device_unregister_subdev(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300171 return 0;
172}
173
174/* ----------------------------------------------------------------------- */
175
Jean Delvareaf294862008-05-18 20:49:40 +0200176static const struct i2c_device_id m52790_id[] = {
177 { "m52790", 0 },
178 { }
179};
180MODULE_DEVICE_TABLE(i2c, m52790_id);
181
Hans Verkuild30f60b2010-09-15 15:18:29 -0300182static struct i2c_driver m52790_driver = {
183 .driver = {
Hans Verkuild30f60b2010-09-15 15:18:29 -0300184 .name = "m52790",
185 },
186 .probe = m52790_probe,
187 .remove = m52790_remove,
188 .id_table = m52790_id,
Hans Verkuil761dacd2007-10-30 05:41:25 -0300189};
Hans Verkuild30f60b2010-09-15 15:18:29 -0300190
Axel Linc6e8d862012-02-12 06:56:32 -0300191module_i2c_driver(m52790_driver);