xref: /openbmc/linux/drivers/media/radio/tef6862.c (revision e2c75e76)
1 /*
2  * tef6862.c Philips TEF6862 Car Radio Enhanced Selectivity Tuner
3  * Copyright (c) 2009 Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-device.h>
24 
25 #define DRIVER_NAME "tef6862"
26 
27 #define FREQ_MUL 16000
28 
29 #define TEF6862_LO_FREQ (875U * FREQ_MUL / 10)
30 #define TEF6862_HI_FREQ (108U * FREQ_MUL)
31 
32 /* Write mode sub addresses */
33 #define WM_SUB_BANDWIDTH	0x0
34 #define WM_SUB_PLLM		0x1
35 #define WM_SUB_PLLL		0x2
36 #define WM_SUB_DAA		0x3
37 #define WM_SUB_AGC		0x4
38 #define WM_SUB_BAND		0x5
39 #define WM_SUB_CONTROL		0x6
40 #define WM_SUB_LEVEL		0x7
41 #define WM_SUB_IFCF		0x8
42 #define WM_SUB_IFCAP		0x9
43 #define WM_SUB_ACD		0xA
44 #define WM_SUB_TEST		0xF
45 
46 /* Different modes of the MSA register */
47 #define MSA_MODE_BUFFER		0x0
48 #define MSA_MODE_PRESET		0x1
49 #define MSA_MODE_SEARCH		0x2
50 #define MSA_MODE_AF_UPDATE	0x3
51 #define MSA_MODE_JUMP		0x4
52 #define MSA_MODE_CHECK		0x5
53 #define MSA_MODE_LOAD		0x6
54 #define MSA_MODE_END		0x7
55 #define MSA_MODE_SHIFT		5
56 
57 struct tef6862_state {
58 	struct v4l2_subdev sd;
59 	unsigned long freq;
60 };
61 
62 static inline struct tef6862_state *to_state(struct v4l2_subdev *sd)
63 {
64 	return container_of(sd, struct tef6862_state, sd);
65 }
66 
67 static u16 tef6862_sigstr(struct i2c_client *client)
68 {
69 	u8 buf[4];
70 	int err = i2c_master_recv(client, buf, sizeof(buf));
71 	if (err == sizeof(buf))
72 		return buf[3] << 8;
73 	return 0;
74 }
75 
76 static int tef6862_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
77 {
78 	if (v->index > 0)
79 		return -EINVAL;
80 
81 	/* only support FM for now */
82 	strlcpy(v->name, "FM", sizeof(v->name));
83 	v->type = V4L2_TUNER_RADIO;
84 	v->rangelow = TEF6862_LO_FREQ;
85 	v->rangehigh = TEF6862_HI_FREQ;
86 	v->rxsubchans = V4L2_TUNER_SUB_MONO;
87 	v->capability = V4L2_TUNER_CAP_LOW;
88 	v->audmode = V4L2_TUNER_MODE_STEREO;
89 	v->signal = tef6862_sigstr(v4l2_get_subdevdata(sd));
90 
91 	return 0;
92 }
93 
94 static int tef6862_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
95 {
96 	return v->index ? -EINVAL : 0;
97 }
98 
99 static int tef6862_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
100 {
101 	struct tef6862_state *state = to_state(sd);
102 	struct i2c_client *client = v4l2_get_subdevdata(sd);
103 	unsigned freq = f->frequency;
104 	u16 pll;
105 	u8 i2cmsg[3];
106 	int err;
107 
108 	if (f->tuner != 0)
109 		return -EINVAL;
110 
111 	freq = clamp(freq, TEF6862_LO_FREQ, TEF6862_HI_FREQ);
112 	pll = 1964 + ((freq - TEF6862_LO_FREQ) * 20) / FREQ_MUL;
113 	i2cmsg[0] = (MSA_MODE_PRESET << MSA_MODE_SHIFT) | WM_SUB_PLLM;
114 	i2cmsg[1] = (pll >> 8) & 0xff;
115 	i2cmsg[2] = pll & 0xff;
116 
117 	err = i2c_master_send(client, i2cmsg, sizeof(i2cmsg));
118 	if (err != sizeof(i2cmsg))
119 		return err < 0 ? err : -EIO;
120 
121 	state->freq = freq;
122 	return 0;
123 }
124 
125 static int tef6862_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
126 {
127 	struct tef6862_state *state = to_state(sd);
128 
129 	if (f->tuner != 0)
130 		return -EINVAL;
131 	f->type = V4L2_TUNER_RADIO;
132 	f->frequency = state->freq;
133 	return 0;
134 }
135 
136 static const struct v4l2_subdev_tuner_ops tef6862_tuner_ops = {
137 	.g_tuner = tef6862_g_tuner,
138 	.s_tuner = tef6862_s_tuner,
139 	.s_frequency = tef6862_s_frequency,
140 	.g_frequency = tef6862_g_frequency,
141 };
142 
143 static const struct v4l2_subdev_ops tef6862_ops = {
144 	.tuner = &tef6862_tuner_ops,
145 };
146 
147 /*
148  * Generic i2c probe
149  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
150  */
151 
152 static int tef6862_probe(struct i2c_client *client,
153 			 const struct i2c_device_id *id)
154 {
155 	struct tef6862_state *state;
156 	struct v4l2_subdev *sd;
157 
158 	/* Check if the adapter supports the needed features */
159 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
160 		return -EIO;
161 
162 	v4l_info(client, "chip found @ 0x%02x (%s)\n",
163 			client->addr << 1, client->adapter->name);
164 
165 	state = kzalloc(sizeof(struct tef6862_state), GFP_KERNEL);
166 	if (state == NULL)
167 		return -ENOMEM;
168 	state->freq = TEF6862_LO_FREQ;
169 
170 	sd = &state->sd;
171 	v4l2_i2c_subdev_init(sd, client, &tef6862_ops);
172 
173 	return 0;
174 }
175 
176 static int tef6862_remove(struct i2c_client *client)
177 {
178 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
179 
180 	v4l2_device_unregister_subdev(sd);
181 	kfree(to_state(sd));
182 	return 0;
183 }
184 
185 static const struct i2c_device_id tef6862_id[] = {
186 	{DRIVER_NAME, 0},
187 	{},
188 };
189 
190 MODULE_DEVICE_TABLE(i2c, tef6862_id);
191 
192 static struct i2c_driver tef6862_driver = {
193 	.driver = {
194 		.name	= DRIVER_NAME,
195 	},
196 	.probe		= tef6862_probe,
197 	.remove		= tef6862_remove,
198 	.id_table	= tef6862_id,
199 };
200 
201 module_i2c_driver(tef6862_driver);
202 
203 MODULE_DESCRIPTION("TEF6862 Car Radio Enhanced Selectivity Tuner");
204 MODULE_AUTHOR("Mocean Laboratories");
205 MODULE_LICENSE("GPL v2");
206