GNU Linux-libre 4.14.257-gnu1
[releases.git] / drivers / media / i2c / uda1342.c
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-device.h>
19 #include <media/i2c/uda1342.h>
20 #include <linux/slab.h>
21
22 static int write_reg(struct i2c_client *client, int reg, int value)
23 {
24         /* UDA1342 wants MSB first, but SMBus sends LSB first */
25         i2c_smbus_write_word_data(client, reg, swab16(value));
26         return 0;
27 }
28
29 static int uda1342_s_routing(struct v4l2_subdev *sd,
30                 u32 input, u32 output, u32 config)
31 {
32         struct i2c_client *client = v4l2_get_subdevdata(sd);
33
34         switch (input) {
35         case UDA1342_IN1:
36                 write_reg(client, 0x00, 0x1241); /* select input 1 */
37                 break;
38         case UDA1342_IN2:
39                 write_reg(client, 0x00, 0x1441); /* select input 2 */
40                 break;
41         default:
42                 v4l2_err(sd, "input %d not supported\n", input);
43                 break;
44         }
45         return 0;
46 }
47
48 static const struct v4l2_subdev_audio_ops uda1342_audio_ops = {
49         .s_routing = uda1342_s_routing,
50 };
51
52 static const struct v4l2_subdev_ops uda1342_ops = {
53         .audio = &uda1342_audio_ops,
54 };
55
56 static int uda1342_probe(struct i2c_client *client,
57                              const struct i2c_device_id *id)
58 {
59         struct i2c_adapter *adapter = client->adapter;
60         struct v4l2_subdev *sd;
61
62         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
63                 return -ENODEV;
64
65         dev_dbg(&client->dev, "initializing UDA1342 at address %d on %s\n",
66                 client->addr, adapter->name);
67
68         sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
69         if (sd == NULL)
70                 return -ENOMEM;
71
72         v4l2_i2c_subdev_init(sd, client, &uda1342_ops);
73
74         write_reg(client, 0x00, 0x8000); /* reset registers */
75         write_reg(client, 0x00, 0x1241); /* select input 1 */
76
77         v4l_info(client, "chip found @ 0x%02x (%s)\n",
78                         client->addr << 1, client->adapter->name);
79
80         return 0;
81 }
82
83 static int uda1342_remove(struct i2c_client *client)
84 {
85         struct v4l2_subdev *sd = i2c_get_clientdata(client);
86
87         v4l2_device_unregister_subdev(sd);
88         return 0;
89 }
90
91 static const struct i2c_device_id uda1342_id[] = {
92         { "uda1342", 0 },
93         { }
94 };
95 MODULE_DEVICE_TABLE(i2c, uda1342_id);
96
97 static struct i2c_driver uda1342_driver = {
98         .driver = {
99                 .name   = "uda1342",
100         },
101         .probe          = uda1342_probe,
102         .remove         = uda1342_remove,
103         .id_table       = uda1342_id,
104 };
105
106 module_i2c_driver(uda1342_driver);
107
108 MODULE_LICENSE("GPL v2");