blob: 9b87067c022cd8f541d94310dd5266ab84cf7c9d [file] [log] [blame]
Boris Brezillon675605c2015-12-31 18:21:21 +01001/*
2 * Copyright (C) 2016 Atmel
3 * Bo Shen <voice.shen@atmel.com>
4 *
5 * Authors: Bo Shen <voice.shen@atmel.com>
6 * Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Wu, Songjun <Songjun.Wu@atmel.com>
8 *
9 *
10 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23#include <linux/gpio/consumer.h>
24#include <linux/i2c.h>
25#include <linux/module.h>
26#include <linux/regmap.h>
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic_helper.h>
30#include <drm/drm_crtc_helper.h>
31#include <drm/drm_edid.h>
32
33#define SII902X_TPI_VIDEO_DATA 0x0
34
35#define SII902X_TPI_PIXEL_REPETITION 0x8
36#define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5)
37#define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4)
38#define SII902X_TPI_AVI_PIXEL_REP_4X 3
39#define SII902X_TPI_AVI_PIXEL_REP_2X 1
40#define SII902X_TPI_AVI_PIXEL_REP_NONE 0
41#define SII902X_TPI_CLK_RATIO_HALF (0 << 6)
42#define SII902X_TPI_CLK_RATIO_1X (1 << 6)
43#define SII902X_TPI_CLK_RATIO_2X (2 << 6)
44#define SII902X_TPI_CLK_RATIO_4X (3 << 6)
45
46#define SII902X_TPI_AVI_IN_FORMAT 0x9
47#define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7)
48#define SII902X_TPI_AVI_INPUT_DITHER BIT(6)
49#define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2)
50#define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2)
51#define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2)
52#define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0)
53#define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0)
54#define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0)
55#define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0)
56
57#define SII902X_TPI_AVI_INFOFRAME 0x0c
58
59#define SII902X_SYS_CTRL_DATA 0x1a
60#define SII902X_SYS_CTRL_PWR_DWN BIT(4)
61#define SII902X_SYS_CTRL_AV_MUTE BIT(3)
62#define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2)
63#define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1)
64#define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0)
65#define SII902X_SYS_CTRL_OUTPUT_HDMI 1
66#define SII902X_SYS_CTRL_OUTPUT_DVI 0
67
68#define SII902X_REG_CHIPID(n) (0x1b + (n))
69
70#define SII902X_PWR_STATE_CTRL 0x1e
71#define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0)
72#define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK)
73
74#define SII902X_INT_ENABLE 0x3c
75#define SII902X_INT_STATUS 0x3d
76#define SII902X_HOTPLUG_EVENT BIT(0)
77#define SII902X_PLUGGED_STATUS BIT(2)
78
79#define SII902X_REG_TPI_RQB 0xc7
80
81#define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500
82
83struct sii902x {
84 struct i2c_client *i2c;
85 struct regmap *regmap;
86 struct drm_bridge bridge;
87 struct drm_connector connector;
88 struct gpio_desc *reset_gpio;
89};
90
91static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge)
92{
93 return container_of(bridge, struct sii902x, bridge);
94}
95
96static inline struct sii902x *connector_to_sii902x(struct drm_connector *con)
97{
98 return container_of(con, struct sii902x, connector);
99}
100
101static void sii902x_reset(struct sii902x *sii902x)
102{
103 if (!sii902x->reset_gpio)
104 return;
105
106 gpiod_set_value(sii902x->reset_gpio, 1);
107
108 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
109 usleep_range(150, 200);
110
111 gpiod_set_value(sii902x->reset_gpio, 0);
112}
113
114static enum drm_connector_status
115sii902x_connector_detect(struct drm_connector *connector, bool force)
116{
117 struct sii902x *sii902x = connector_to_sii902x(connector);
118 unsigned int status;
119
120 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
121
122 return (status & SII902X_PLUGGED_STATUS) ?
123 connector_status_connected : connector_status_disconnected;
124}
125
126static const struct drm_connector_funcs sii902x_connector_funcs = {
127 .dpms = drm_atomic_helper_connector_dpms,
128 .detect = sii902x_connector_detect,
129 .fill_modes = drm_helper_probe_single_connector_modes,
130 .destroy = drm_connector_cleanup,
131 .reset = drm_atomic_helper_connector_reset,
132 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
133 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
134};
135
136static int sii902x_get_modes(struct drm_connector *connector)
137{
138 struct sii902x *sii902x = connector_to_sii902x(connector);
139 struct regmap *regmap = sii902x->regmap;
140 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
141 unsigned long timeout;
142 unsigned int status;
143 struct edid *edid;
144 int num = 0;
145 int ret;
146
147 ret = regmap_update_bits(regmap, SII902X_SYS_CTRL_DATA,
148 SII902X_SYS_CTRL_DDC_BUS_REQ,
149 SII902X_SYS_CTRL_DDC_BUS_REQ);
150 if (ret)
151 return ret;
152
153 timeout = jiffies +
154 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
155 do {
156 ret = regmap_read(regmap, SII902X_SYS_CTRL_DATA, &status);
157 if (ret)
158 return ret;
159 } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
160 time_before(jiffies, timeout));
161
162 if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
Liu Yingce9971d2017-04-11 10:22:19 +0800163 dev_err(&sii902x->i2c->dev, "failed to acquire the i2c bus\n");
Boris Brezillon675605c2015-12-31 18:21:21 +0100164 return -ETIMEDOUT;
165 }
166
167 ret = regmap_write(regmap, SII902X_SYS_CTRL_DATA, status);
168 if (ret)
169 return ret;
170
171 edid = drm_get_edid(connector, sii902x->i2c->adapter);
172 drm_mode_connector_update_edid_property(connector, edid);
173 if (edid) {
174 num = drm_add_edid_modes(connector, edid);
175 kfree(edid);
176 }
177
178 ret = drm_display_info_set_bus_formats(&connector->display_info,
179 &bus_format, 1);
180 if (ret)
181 return ret;
182
183 ret = regmap_read(regmap, SII902X_SYS_CTRL_DATA, &status);
184 if (ret)
185 return ret;
186
187 ret = regmap_update_bits(regmap, SII902X_SYS_CTRL_DATA,
188 SII902X_SYS_CTRL_DDC_BUS_REQ |
189 SII902X_SYS_CTRL_DDC_BUS_GRTD, 0);
190 if (ret)
191 return ret;
192
193 timeout = jiffies +
194 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
195 do {
196 ret = regmap_read(regmap, SII902X_SYS_CTRL_DATA, &status);
197 if (ret)
198 return ret;
199 } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
200 SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
201 time_before(jiffies, timeout));
202
203 if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
204 SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
Liu Yingce9971d2017-04-11 10:22:19 +0800205 dev_err(&sii902x->i2c->dev, "failed to release the i2c bus\n");
Boris Brezillon675605c2015-12-31 18:21:21 +0100206 return -ETIMEDOUT;
207 }
208
209 return num;
210}
211
212static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector,
213 struct drm_display_mode *mode)
214{
215 /* TODO: check mode */
216
217 return MODE_OK;
218}
219
220static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = {
221 .get_modes = sii902x_get_modes,
222 .mode_valid = sii902x_mode_valid,
223};
224
225static void sii902x_bridge_disable(struct drm_bridge *bridge)
226{
227 struct sii902x *sii902x = bridge_to_sii902x(bridge);
228
229 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
230 SII902X_SYS_CTRL_PWR_DWN,
231 SII902X_SYS_CTRL_PWR_DWN);
232}
233
234static void sii902x_bridge_enable(struct drm_bridge *bridge)
235{
236 struct sii902x *sii902x = bridge_to_sii902x(bridge);
237
238 regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL,
239 SII902X_AVI_POWER_STATE_MSK,
240 SII902X_AVI_POWER_STATE_D(0));
241 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
242 SII902X_SYS_CTRL_PWR_DWN, 0);
243}
244
245static void sii902x_bridge_mode_set(struct drm_bridge *bridge,
246 struct drm_display_mode *mode,
247 struct drm_display_mode *adj)
248{
249 struct sii902x *sii902x = bridge_to_sii902x(bridge);
250 struct regmap *regmap = sii902x->regmap;
251 u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
252 struct hdmi_avi_infoframe frame;
253 int ret;
254
255 buf[0] = adj->clock;
256 buf[1] = adj->clock >> 8;
257 buf[2] = adj->vrefresh;
258 buf[3] = 0x00;
259 buf[4] = adj->hdisplay;
260 buf[5] = adj->hdisplay >> 8;
261 buf[6] = adj->vdisplay;
262 buf[7] = adj->vdisplay >> 8;
263 buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE |
264 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT;
265 buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO |
266 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB;
267
268 ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10);
269 if (ret)
270 return;
271
272 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, adj);
273 if (ret < 0) {
274 DRM_ERROR("couldn't fill AVI infoframe\n");
275 return;
276 }
277
278 ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf));
279 if (ret < 0) {
280 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
281 return;
282 }
283
284 /* Do not send the infoframe header, but keep the CRC field. */
285 regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME,
286 buf + HDMI_INFOFRAME_HEADER_SIZE - 1,
287 HDMI_AVI_INFOFRAME_SIZE + 1);
288}
289
290static int sii902x_bridge_attach(struct drm_bridge *bridge)
291{
292 struct sii902x *sii902x = bridge_to_sii902x(bridge);
293 struct drm_device *drm = bridge->dev;
294 int ret;
295
296 drm_connector_helper_add(&sii902x->connector,
297 &sii902x_connector_helper_funcs);
298
299 if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) {
300 dev_err(&sii902x->i2c->dev,
Liu Yingce9971d2017-04-11 10:22:19 +0800301 "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
Boris Brezillon675605c2015-12-31 18:21:21 +0100302 return -ENOTSUPP;
303 }
304
305 ret = drm_connector_init(drm, &sii902x->connector,
306 &sii902x_connector_funcs,
307 DRM_MODE_CONNECTOR_HDMIA);
308 if (ret)
309 return ret;
310
311 if (sii902x->i2c->irq > 0)
312 sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD;
313 else
314 sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
315
316 drm_mode_connector_attach_encoder(&sii902x->connector, bridge->encoder);
317
318 return 0;
319}
320
321static const struct drm_bridge_funcs sii902x_bridge_funcs = {
322 .attach = sii902x_bridge_attach,
323 .mode_set = sii902x_bridge_mode_set,
324 .disable = sii902x_bridge_disable,
325 .enable = sii902x_bridge_enable,
326};
327
328static const struct regmap_range sii902x_volatile_ranges[] = {
329 { .range_min = 0, .range_max = 0xff },
330};
331
332static const struct regmap_access_table sii902x_volatile_table = {
333 .yes_ranges = sii902x_volatile_ranges,
334 .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges),
335};
336
337static const struct regmap_config sii902x_regmap_config = {
338 .reg_bits = 8,
339 .val_bits = 8,
340 .volatile_table = &sii902x_volatile_table,
341 .cache_type = REGCACHE_NONE,
342};
343
344static irqreturn_t sii902x_interrupt(int irq, void *data)
345{
346 struct sii902x *sii902x = data;
347 unsigned int status = 0;
348
349 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
350 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
351
352 if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev)
353 drm_helper_hpd_irq_event(sii902x->bridge.dev);
354
355 return IRQ_HANDLED;
356}
357
358static int sii902x_probe(struct i2c_client *client,
359 const struct i2c_device_id *id)
360{
361 struct device *dev = &client->dev;
362 unsigned int status = 0;
363 struct sii902x *sii902x;
364 u8 chipid[4];
365 int ret;
366
367 sii902x = devm_kzalloc(dev, sizeof(*sii902x), GFP_KERNEL);
368 if (!sii902x)
369 return -ENOMEM;
370
371 sii902x->i2c = client;
372 sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config);
373 if (IS_ERR(sii902x->regmap))
374 return PTR_ERR(sii902x->regmap);
375
376 sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset",
377 GPIOD_OUT_LOW);
378 if (IS_ERR(sii902x->reset_gpio)) {
379 dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n",
380 PTR_ERR(sii902x->reset_gpio));
381 return PTR_ERR(sii902x->reset_gpio);
382 }
383
384 sii902x_reset(sii902x);
385
386 ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0);
387 if (ret)
388 return ret;
389
390 ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0),
391 &chipid, 4);
392 if (ret) {
393 dev_err(dev, "regmap_read failed %d\n", ret);
394 return ret;
395 }
396
397 if (chipid[0] != 0xb0) {
398 dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n",
399 chipid[0]);
400 return -EINVAL;
401 }
402
403 /* Clear all pending interrupts */
404 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
405 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
406
407 if (client->irq > 0) {
408 regmap_write(sii902x->regmap, SII902X_INT_ENABLE,
409 SII902X_HOTPLUG_EVENT);
410
411 ret = devm_request_threaded_irq(dev, client->irq, NULL,
412 sii902x_interrupt,
413 IRQF_ONESHOT, dev_name(dev),
414 sii902x);
415 if (ret)
416 return ret;
417 }
418
419 sii902x->bridge.funcs = &sii902x_bridge_funcs;
420 sii902x->bridge.of_node = dev->of_node;
421 ret = drm_bridge_add(&sii902x->bridge);
422 if (ret) {
423 dev_err(dev, "Failed to add drm_bridge\n");
424 return ret;
425 }
426
427 i2c_set_clientdata(client, sii902x);
428
429 return 0;
430}
431
432static int sii902x_remove(struct i2c_client *client)
433
434{
435 struct sii902x *sii902x = i2c_get_clientdata(client);
436
437 drm_bridge_remove(&sii902x->bridge);
438
439 return 0;
440}
441
442static const struct of_device_id sii902x_dt_ids[] = {
443 { .compatible = "sil,sii9022", },
444 { }
445};
446MODULE_DEVICE_TABLE(of, sii902x_dt_ids);
447
448static const struct i2c_device_id sii902x_i2c_ids[] = {
449 { "sii9022", 0 },
450 { },
451};
452MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids);
453
454static struct i2c_driver sii902x_driver = {
455 .probe = sii902x_probe,
456 .remove = sii902x_remove,
457 .driver = {
458 .name = "sii902x",
459 .of_match_table = sii902x_dt_ids,
460 },
461 .id_table = sii902x_i2c_ids,
462};
463module_i2c_driver(sii902x_driver);
464
465MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
466MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
467MODULE_LICENSE("GPL");