rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / media / platform / s5p-tv / sii9234_drv.c
blob39b77d24b9c276b3ef1d0dff2b0fed6f4cca72c1
1 /*
2 * Samsung MHL interface driver
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Tomasz Stanislawski <t.stanislaws@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/freezer.h>
16 #include <linux/gpio.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/kthread.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/slab.h>
26 #include <media/sii9234.h>
27 #include <media/v4l2-subdev.h>
29 MODULE_AUTHOR("Tomasz Stanislawski <t.stanislaws@samsung.com>");
30 MODULE_DESCRIPTION("Samsung MHL interface driver");
31 MODULE_LICENSE("GPL");
33 struct sii9234_context {
34 struct i2c_client *client;
35 struct regulator *power;
36 int gpio_n_reset;
37 struct v4l2_subdev sd;
40 static inline struct sii9234_context *sd_to_context(struct v4l2_subdev *sd)
42 return container_of(sd, struct sii9234_context, sd);
45 static inline int sii9234_readb(struct i2c_client *client, int addr)
47 return i2c_smbus_read_byte_data(client, addr);
50 static inline int sii9234_writeb(struct i2c_client *client, int addr, int value)
52 return i2c_smbus_write_byte_data(client, addr, value);
55 static inline int sii9234_writeb_mask(struct i2c_client *client, int addr,
56 int value, int mask)
58 int ret;
60 ret = i2c_smbus_read_byte_data(client, addr);
61 if (ret < 0)
62 return ret;
63 ret = (ret & ~mask) | (value & mask);
64 return i2c_smbus_write_byte_data(client, addr, ret);
67 static inline int sii9234_readb_idx(struct i2c_client *client, int addr)
69 int ret;
70 ret = i2c_smbus_write_byte_data(client, 0xbc, addr >> 8);
71 if (ret < 0)
72 return ret;
73 ret = i2c_smbus_write_byte_data(client, 0xbd, addr & 0xff);
74 if (ret < 0)
75 return ret;
76 return i2c_smbus_read_byte_data(client, 0xbe);
79 static inline int sii9234_writeb_idx(struct i2c_client *client, int addr,
80 int value)
82 int ret;
83 ret = i2c_smbus_write_byte_data(client, 0xbc, addr >> 8);
84 if (ret < 0)
85 return ret;
86 ret = i2c_smbus_write_byte_data(client, 0xbd, addr & 0xff);
87 if (ret < 0)
88 return ret;
89 ret = i2c_smbus_write_byte_data(client, 0xbe, value);
90 return ret;
93 static inline int sii9234_writeb_idx_mask(struct i2c_client *client, int addr,
94 int value, int mask)
96 int ret;
98 ret = sii9234_readb_idx(client, addr);
99 if (ret < 0)
100 return ret;
101 ret = (ret & ~mask) | (value & mask);
102 return sii9234_writeb_idx(client, addr, ret);
105 static int sii9234_reset(struct sii9234_context *ctx)
107 struct i2c_client *client = ctx->client;
108 struct device *dev = &client->dev;
109 int ret, tries;
111 gpio_direction_output(ctx->gpio_n_reset, 1);
112 mdelay(1);
113 gpio_direction_output(ctx->gpio_n_reset, 0);
114 mdelay(1);
115 gpio_direction_output(ctx->gpio_n_reset, 1);
116 mdelay(1);
118 /* going to TTPI mode */
119 ret = sii9234_writeb(client, 0xc7, 0);
120 if (ret < 0) {
121 dev_err(dev, "failed to set TTPI mode\n");
122 return ret;
124 for (tries = 0; tries < 100 ; ++tries) {
125 ret = sii9234_readb(client, 0x1b);
126 if (ret > 0)
127 break;
128 if (ret < 0) {
129 dev_err(dev, "failed to reset device\n");
130 return -EIO;
132 mdelay(1);
134 if (tries == 100) {
135 dev_err(dev, "maximal number of tries reached\n");
136 return -EIO;
139 return 0;
142 static int sii9234_verify_version(struct i2c_client *client)
144 struct device *dev = &client->dev;
145 int family, rev, tpi_rev, dev_id, sub_id, hdcp, id;
147 family = sii9234_readb(client, 0x1b);
148 rev = sii9234_readb(client, 0x1c) & 0x0f;
149 tpi_rev = sii9234_readb(client, 0x1d) & 0x7f;
150 dev_id = sii9234_readb_idx(client, 0x0103);
151 sub_id = sii9234_readb_idx(client, 0x0102);
152 hdcp = sii9234_readb(client, 0x30);
154 if (family < 0 || rev < 0 || tpi_rev < 0 || dev_id < 0 ||
155 sub_id < 0 || hdcp < 0) {
156 dev_err(dev, "failed to read chip's version\n");
157 return -EIO;
160 id = (dev_id << 8) | sub_id;
162 dev_info(dev, "chip: SiL%02x family: %02x, rev: %02x\n",
163 id, family, rev);
164 dev_info(dev, "tpi_rev:%02x, hdcp: %02x\n", tpi_rev, hdcp);
165 if (id != 0x9234) {
166 dev_err(dev, "not supported chip\n");
167 return -ENODEV;
170 return 0;
173 static u8 data[][3] = {
174 /* setup from driver created by doonsoo45.kim */
175 { 0x01, 0x05, 0x04 }, /* Enable Auto soft reset on SCDT = 0 */
176 { 0x01, 0x08, 0x35 }, /* Power Up TMDS Tx Core */
177 { 0x01, 0x0d, 0x1c }, /* HDMI Transcode mode enable */
178 { 0x01, 0x2b, 0x01 }, /* Enable HDCP Compliance workaround */
179 { 0x01, 0x79, 0x40 }, /* daniel test...MHL_INT */
180 { 0x01, 0x80, 0x34 }, /* Enable Rx PLL Clock Value */
181 { 0x01, 0x90, 0x27 }, /* Enable CBUS discovery */
182 { 0x01, 0x91, 0xe5 }, /* Skip RGND detection */
183 { 0x01, 0x92, 0x46 }, /* Force MHD mode */
184 { 0x01, 0x93, 0xdc }, /* Disable CBUS pull-up during RGND measurement */
185 { 0x01, 0x94, 0x66 }, /* 1.8V CBUS VTH & GND threshold */
186 { 0x01, 0x95, 0x31 }, /* RGND block & single discovery attempt */
187 { 0x01, 0x96, 0x22 }, /* use 1K and 2K setting */
188 { 0x01, 0xa0, 0x10 }, /* SIMG: Term mode */
189 { 0x01, 0xa1, 0xfc }, /* Disable internal Mobile HD driver */
190 { 0x01, 0xa3, 0xfa }, /* SIMG: Output Swing default EB, 3x Clk Mult */
191 { 0x01, 0xa5, 0x80 }, /* SIMG: RGND Hysterisis, 3x mode for Beast */
192 { 0x01, 0xa6, 0x0c }, /* SIMG: Swing Offset */
193 { 0x02, 0x3d, 0x3f }, /* Power up CVCC 1.2V core */
194 { 0x03, 0x00, 0x00 }, /* SIMG: correcting HW default */
195 { 0x03, 0x11, 0x01 }, /* Enable TxPLL Clock */
196 { 0x03, 0x12, 0x15 }, /* Enable Tx Clock Path & Equalizer */
197 { 0x03, 0x13, 0x60 }, /* SIMG: Set termination value */
198 { 0x03, 0x14, 0xf0 }, /* SIMG: Change CKDT level */
199 { 0x03, 0x17, 0x07 }, /* SIMG: PLL Calrefsel */
200 { 0x03, 0x1a, 0x20 }, /* VCO Cal */
201 { 0x03, 0x22, 0xe0 }, /* SIMG: Auto EQ */
202 { 0x03, 0x23, 0xc0 }, /* SIMG: Auto EQ */
203 { 0x03, 0x24, 0xa0 }, /* SIMG: Auto EQ */
204 { 0x03, 0x25, 0x80 }, /* SIMG: Auto EQ */
205 { 0x03, 0x26, 0x60 }, /* SIMG: Auto EQ */
206 { 0x03, 0x27, 0x40 }, /* SIMG: Auto EQ */
207 { 0x03, 0x28, 0x20 }, /* SIMG: Auto EQ */
208 { 0x03, 0x29, 0x00 }, /* SIMG: Auto EQ */
209 { 0x03, 0x31, 0x0b }, /* SIMG: Rx PLL BW value from I2C BW ~ 4MHz */
210 { 0x03, 0x45, 0x06 }, /* SIMG: DPLL Mode */
211 { 0x03, 0x4b, 0x06 }, /* SIMG: Correcting HW default */
212 { 0x03, 0x4c, 0xa0 }, /* Manual zone control */
213 { 0x03, 0x4d, 0x02 }, /* SIMG: PLL Mode Value (order is important) */
216 static int sii9234_set_internal(struct sii9234_context *ctx)
218 struct i2c_client *client = ctx->client;
219 int i, ret;
221 for (i = 0; i < ARRAY_SIZE(data); ++i) {
222 int addr = (data[i][0] << 8) | data[i][1];
223 ret = sii9234_writeb_idx(client, addr, data[i][2]);
224 if (ret < 0)
225 return ret;
227 return 0;
230 static int sii9234_runtime_suspend(struct device *dev)
232 struct v4l2_subdev *sd = dev_get_drvdata(dev);
233 struct sii9234_context *ctx = sd_to_context(sd);
234 struct i2c_client *client = ctx->client;
236 dev_info(dev, "suspend start\n");
238 sii9234_writeb_mask(client, 0x1e, 3, 3);
239 regulator_disable(ctx->power);
241 return 0;
244 static int sii9234_runtime_resume(struct device *dev)
246 struct v4l2_subdev *sd = dev_get_drvdata(dev);
247 struct sii9234_context *ctx = sd_to_context(sd);
248 struct i2c_client *client = ctx->client;
249 int ret;
251 dev_info(dev, "resume start\n");
252 regulator_enable(ctx->power);
254 ret = sii9234_reset(ctx);
255 if (ret)
256 goto fail;
258 /* enable tpi */
259 ret = sii9234_writeb_mask(client, 0x1e, 1, 0);
260 if (ret < 0)
261 goto fail;
262 ret = sii9234_set_internal(ctx);
263 if (ret < 0)
264 goto fail;
266 return 0;
268 fail:
269 dev_err(dev, "failed to resume\n");
270 regulator_disable(ctx->power);
272 return ret;
275 static const struct dev_pm_ops sii9234_pm_ops = {
276 .runtime_suspend = sii9234_runtime_suspend,
277 .runtime_resume = sii9234_runtime_resume,
280 static int sii9234_s_power(struct v4l2_subdev *sd, int on)
282 struct sii9234_context *ctx = sd_to_context(sd);
283 int ret;
285 if (on)
286 ret = pm_runtime_get_sync(&ctx->client->dev);
287 else
288 ret = pm_runtime_put(&ctx->client->dev);
289 /* only values < 0 indicate errors */
290 return IS_ERR_VALUE(ret) ? ret : 0;
293 static int sii9234_s_stream(struct v4l2_subdev *sd, int enable)
295 struct sii9234_context *ctx = sd_to_context(sd);
297 /* (dis/en)able TDMS output */
298 sii9234_writeb_mask(ctx->client, 0x1a, enable ? 0 : ~0 , 1 << 4);
299 return 0;
302 static const struct v4l2_subdev_core_ops sii9234_core_ops = {
303 .s_power = sii9234_s_power,
306 static const struct v4l2_subdev_video_ops sii9234_video_ops = {
307 .s_stream = sii9234_s_stream,
310 static const struct v4l2_subdev_ops sii9234_ops = {
311 .core = &sii9234_core_ops,
312 .video = &sii9234_video_ops,
315 static int sii9234_probe(struct i2c_client *client,
316 const struct i2c_device_id *id)
318 struct device *dev = &client->dev;
319 struct sii9234_platform_data *pdata = dev->platform_data;
320 struct sii9234_context *ctx;
321 int ret;
323 ctx = devm_kzalloc(&client->dev, sizeof(*ctx), GFP_KERNEL);
324 if (!ctx) {
325 dev_err(dev, "out of memory\n");
326 ret = -ENOMEM;
327 goto fail;
329 ctx->client = client;
331 ctx->power = devm_regulator_get(dev, "hdmi-en");
332 if (IS_ERR(ctx->power)) {
333 dev_err(dev, "failed to acquire regulator hdmi-en\n");
334 return PTR_ERR(ctx->power);
337 ctx->gpio_n_reset = pdata->gpio_n_reset;
338 ret = devm_gpio_request(dev, ctx->gpio_n_reset, "MHL_RST");
339 if (ret) {
340 dev_err(dev, "failed to acquire MHL_RST gpio\n");
341 return ret;
344 v4l2_i2c_subdev_init(&ctx->sd, client, &sii9234_ops);
346 pm_runtime_enable(dev);
348 /* enable device */
349 ret = pm_runtime_get_sync(dev);
350 if (ret)
351 goto fail_pm;
353 /* verify chip version */
354 ret = sii9234_verify_version(client);
355 if (ret)
356 goto fail_pm_get;
358 /* stop processing */
359 pm_runtime_put(dev);
361 dev_info(dev, "probe successful\n");
363 return 0;
365 fail_pm_get:
366 pm_runtime_put_sync(dev);
368 fail_pm:
369 pm_runtime_disable(dev);
371 fail:
372 dev_err(dev, "probe failed\n");
374 return ret;
377 static int sii9234_remove(struct i2c_client *client)
379 struct device *dev = &client->dev;
381 pm_runtime_disable(dev);
383 dev_info(dev, "remove successful\n");
385 return 0;
389 static const struct i2c_device_id sii9234_id[] = {
390 { "SII9234", 0 },
391 { },
394 MODULE_DEVICE_TABLE(i2c, sii9234_id);
395 static struct i2c_driver sii9234_driver = {
396 .driver = {
397 .name = "sii9234",
398 .owner = THIS_MODULE,
399 .pm = &sii9234_pm_ops,
401 .probe = sii9234_probe,
402 .remove = sii9234_remove,
403 .id_table = sii9234_id,
406 module_i2c_driver(sii9234_driver);