]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/bridge/sii902x.c
Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / bridge / sii902x.c
CommitLineData
675605c1 1/*
21d80840
FC
2 * Copyright (C) 2018 Renesas Electronics
3 *
675605c1
BB
4 * Copyright (C) 2016 Atmel
5 * Bo Shen <voice.shen@atmel.com>
6 *
7 * Authors: Bo Shen <voice.shen@atmel.com>
8 * Boris Brezillon <boris.brezillon@free-electrons.com>
9 * Wu, Songjun <Songjun.Wu@atmel.com>
10 *
11 *
12 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25#include <linux/gpio/consumer.h>
21d80840 26#include <linux/i2c-mux.h>
675605c1
BB
27#include <linux/i2c.h>
28#include <linux/module.h>
29#include <linux/regmap.h>
30
31#include <drm/drmP.h>
32#include <drm/drm_atomic_helper.h>
675605c1 33#include <drm/drm_edid.h>
fcd70cd3 34#include <drm/drm_probe_helper.h>
675605c1
BB
35
36#define SII902X_TPI_VIDEO_DATA 0x0
37
38#define SII902X_TPI_PIXEL_REPETITION 0x8
39#define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5)
40#define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4)
41#define SII902X_TPI_AVI_PIXEL_REP_4X 3
42#define SII902X_TPI_AVI_PIXEL_REP_2X 1
43#define SII902X_TPI_AVI_PIXEL_REP_NONE 0
44#define SII902X_TPI_CLK_RATIO_HALF (0 << 6)
45#define SII902X_TPI_CLK_RATIO_1X (1 << 6)
46#define SII902X_TPI_CLK_RATIO_2X (2 << 6)
47#define SII902X_TPI_CLK_RATIO_4X (3 << 6)
48
49#define SII902X_TPI_AVI_IN_FORMAT 0x9
50#define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7)
51#define SII902X_TPI_AVI_INPUT_DITHER BIT(6)
52#define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2)
53#define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2)
54#define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2)
55#define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0)
56#define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0)
57#define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0)
58#define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0)
59
60#define SII902X_TPI_AVI_INFOFRAME 0x0c
61
62#define SII902X_SYS_CTRL_DATA 0x1a
63#define SII902X_SYS_CTRL_PWR_DWN BIT(4)
64#define SII902X_SYS_CTRL_AV_MUTE BIT(3)
65#define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2)
66#define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1)
67#define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0)
68#define SII902X_SYS_CTRL_OUTPUT_HDMI 1
69#define SII902X_SYS_CTRL_OUTPUT_DVI 0
70
71#define SII902X_REG_CHIPID(n) (0x1b + (n))
72
73#define SII902X_PWR_STATE_CTRL 0x1e
74#define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0)
75#define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK)
76
77#define SII902X_INT_ENABLE 0x3c
78#define SII902X_INT_STATUS 0x3d
79#define SII902X_HOTPLUG_EVENT BIT(0)
80#define SII902X_PLUGGED_STATUS BIT(2)
81
82#define SII902X_REG_TPI_RQB 0xc7
83
84#define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500
85
86struct sii902x {
87 struct i2c_client *i2c;
88 struct regmap *regmap;
89 struct drm_bridge bridge;
90 struct drm_connector connector;
91 struct gpio_desc *reset_gpio;
21d80840 92 struct i2c_mux_core *i2cmux;
675605c1
BB
93};
94
21d80840
FC
95static int sii902x_read_unlocked(struct i2c_client *i2c, u8 reg, u8 *val)
96{
97 union i2c_smbus_data data;
98 int ret;
99
100 ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
101 I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data);
102
103 if (ret < 0)
104 return ret;
105
106 *val = data.byte;
107 return 0;
108}
109
110static int sii902x_write_unlocked(struct i2c_client *i2c, u8 reg, u8 val)
111{
112 union i2c_smbus_data data;
113
114 data.byte = val;
115
116 return __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
117 I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA,
118 &data);
119}
120
121static int sii902x_update_bits_unlocked(struct i2c_client *i2c, u8 reg, u8 mask,
122 u8 val)
123{
124 int ret;
125 u8 status;
126
127 ret = sii902x_read_unlocked(i2c, reg, &status);
128 if (ret)
129 return ret;
130 status &= ~mask;
131 status |= val & mask;
132 return sii902x_write_unlocked(i2c, reg, status);
133}
134
675605c1
BB
135static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge)
136{
137 return container_of(bridge, struct sii902x, bridge);
138}
139
140static inline struct sii902x *connector_to_sii902x(struct drm_connector *con)
141{
142 return container_of(con, struct sii902x, connector);
143}
144
145static void sii902x_reset(struct sii902x *sii902x)
146{
147 if (!sii902x->reset_gpio)
148 return;
149
150 gpiod_set_value(sii902x->reset_gpio, 1);
151
152 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
153 usleep_range(150, 200);
154
155 gpiod_set_value(sii902x->reset_gpio, 0);
156}
157
158static enum drm_connector_status
159sii902x_connector_detect(struct drm_connector *connector, bool force)
160{
161 struct sii902x *sii902x = connector_to_sii902x(connector);
162 unsigned int status;
163
164 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
165
166 return (status & SII902X_PLUGGED_STATUS) ?
167 connector_status_connected : connector_status_disconnected;
168}
169
170static const struct drm_connector_funcs sii902x_connector_funcs = {
675605c1
BB
171 .detect = sii902x_connector_detect,
172 .fill_modes = drm_helper_probe_single_connector_modes,
173 .destroy = drm_connector_cleanup,
174 .reset = drm_atomic_helper_connector_reset,
175 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
176 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
177};
178
179static int sii902x_get_modes(struct drm_connector *connector)
180{
181 struct sii902x *sii902x = connector_to_sii902x(connector);
675605c1 182 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
675605c1 183 struct edid *edid;
21d80840 184 int num = 0, ret;
675605c1 185
21d80840 186 edid = drm_get_edid(connector, sii902x->i2cmux->adapter[0]);
c555f023 187 drm_connector_update_edid_property(connector, edid);
675605c1
BB
188 if (edid) {
189 num = drm_add_edid_modes(connector, edid);
190 kfree(edid);
191 }
192
193 ret = drm_display_info_set_bus_formats(&connector->display_info,
194 &bus_format, 1);
195 if (ret)
196 return ret;
197
675605c1
BB
198 return num;
199}
200
201static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector,
202 struct drm_display_mode *mode)
203{
204 /* TODO: check mode */
205
206 return MODE_OK;
207}
208
209static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = {
210 .get_modes = sii902x_get_modes,
211 .mode_valid = sii902x_mode_valid,
212};
213
214static void sii902x_bridge_disable(struct drm_bridge *bridge)
215{
216 struct sii902x *sii902x = bridge_to_sii902x(bridge);
217
218 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
219 SII902X_SYS_CTRL_PWR_DWN,
220 SII902X_SYS_CTRL_PWR_DWN);
221}
222
223static void sii902x_bridge_enable(struct drm_bridge *bridge)
224{
225 struct sii902x *sii902x = bridge_to_sii902x(bridge);
226
227 regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL,
228 SII902X_AVI_POWER_STATE_MSK,
229 SII902X_AVI_POWER_STATE_D(0));
230 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
231 SII902X_SYS_CTRL_PWR_DWN, 0);
232}
233
234static void sii902x_bridge_mode_set(struct drm_bridge *bridge,
63f8f3ba
LP
235 const struct drm_display_mode *mode,
236 const struct drm_display_mode *adj)
675605c1
BB
237{
238 struct sii902x *sii902x = bridge_to_sii902x(bridge);
239 struct regmap *regmap = sii902x->regmap;
240 u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
241 struct hdmi_avi_infoframe frame;
242 int ret;
243
244 buf[0] = adj->clock;
245 buf[1] = adj->clock >> 8;
246 buf[2] = adj->vrefresh;
247 buf[3] = 0x00;
248 buf[4] = adj->hdisplay;
249 buf[5] = adj->hdisplay >> 8;
250 buf[6] = adj->vdisplay;
251 buf[7] = adj->vdisplay >> 8;
252 buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE |
253 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT;
254 buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO |
255 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB;
256
257 ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10);
258 if (ret)
259 return;
260
13d0add3
VS
261 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame,
262 &sii902x->connector, adj);
675605c1
BB
263 if (ret < 0) {
264 DRM_ERROR("couldn't fill AVI infoframe\n");
265 return;
266 }
267
268 ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf));
269 if (ret < 0) {
270 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
271 return;
272 }
273
274 /* Do not send the infoframe header, but keep the CRC field. */
275 regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME,
276 buf + HDMI_INFOFRAME_HEADER_SIZE - 1,
277 HDMI_AVI_INFOFRAME_SIZE + 1);
278}
279
280static int sii902x_bridge_attach(struct drm_bridge *bridge)
281{
282 struct sii902x *sii902x = bridge_to_sii902x(bridge);
283 struct drm_device *drm = bridge->dev;
284 int ret;
285
286 drm_connector_helper_add(&sii902x->connector,
287 &sii902x_connector_helper_funcs);
288
289 if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) {
290 dev_err(&sii902x->i2c->dev,
ce9971de 291 "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
675605c1
BB
292 return -ENOTSUPP;
293 }
294
295 ret = drm_connector_init(drm, &sii902x->connector,
296 &sii902x_connector_funcs,
297 DRM_MODE_CONNECTOR_HDMIA);
298 if (ret)
299 return ret;
300
301 if (sii902x->i2c->irq > 0)
302 sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD;
303 else
304 sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
305
cde4c44d 306 drm_connector_attach_encoder(&sii902x->connector, bridge->encoder);
675605c1
BB
307
308 return 0;
309}
310
311static const struct drm_bridge_funcs sii902x_bridge_funcs = {
312 .attach = sii902x_bridge_attach,
313 .mode_set = sii902x_bridge_mode_set,
314 .disable = sii902x_bridge_disable,
315 .enable = sii902x_bridge_enable,
316};
317
318static const struct regmap_range sii902x_volatile_ranges[] = {
319 { .range_min = 0, .range_max = 0xff },
320};
321
322static const struct regmap_access_table sii902x_volatile_table = {
323 .yes_ranges = sii902x_volatile_ranges,
324 .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges),
325};
326
327static const struct regmap_config sii902x_regmap_config = {
328 .reg_bits = 8,
329 .val_bits = 8,
330 .volatile_table = &sii902x_volatile_table,
331 .cache_type = REGCACHE_NONE,
332};
333
334static irqreturn_t sii902x_interrupt(int irq, void *data)
335{
336 struct sii902x *sii902x = data;
337 unsigned int status = 0;
338
339 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
340 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
341
342 if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev)
343 drm_helper_hpd_irq_event(sii902x->bridge.dev);
344
345 return IRQ_HANDLED;
346}
347
21d80840
FC
348/*
349 * The purpose of sii902x_i2c_bypass_select is to enable the pass through
350 * mode of the HDMI transmitter. Do not use regmap from within this function,
351 * only use sii902x_*_unlocked functions to read/modify/write registers.
352 * We are holding the parent adapter lock here, keep this in mind before
353 * adding more i2c transactions.
354 *
355 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
356 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
357 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
358 * we leave the remaining bits as we have found them.
359 */
360static int sii902x_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id)
361{
362 struct sii902x *sii902x = i2c_mux_priv(mux);
363 struct device *dev = &sii902x->i2c->dev;
364 unsigned long timeout;
365 u8 status;
366 int ret;
367
368 ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
369 SII902X_SYS_CTRL_DDC_BUS_REQ,
370 SII902X_SYS_CTRL_DDC_BUS_REQ);
371 if (ret)
372 return ret;
373
374 timeout = jiffies +
375 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
376 do {
377 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
378 &status);
379 if (ret)
380 return ret;
381 } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
382 time_before(jiffies, timeout));
383
384 if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
385 dev_err(dev, "Failed to acquire the i2c bus\n");
386 return -ETIMEDOUT;
387 }
388
389 return sii902x_write_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
390 status);
391}
392
393/*
394 * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through
395 * mode of the HDMI transmitter. Do not use regmap from within this function,
396 * only use sii902x_*_unlocked functions to read/modify/write registers.
397 * We are holding the parent adapter lock here, keep this in mind before
398 * adding more i2c transactions.
399 *
400 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
401 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
402 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
403 * we leave the remaining bits as we have found them.
404 */
405static int sii902x_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id)
406{
407 struct sii902x *sii902x = i2c_mux_priv(mux);
408 struct device *dev = &sii902x->i2c->dev;
409 unsigned long timeout;
410 unsigned int retries;
411 u8 status;
412 int ret;
413
414 /*
415 * When the HDMI transmitter is in pass through mode, we need an
416 * (undocumented) additional delay between STOP and START conditions
417 * to guarantee the bus won't get stuck.
418 */
419 udelay(30);
420
421 /*
422 * Sometimes the I2C bus can stall after failure to use the
423 * EDID channel. Retry a few times to see if things clear
424 * up, else continue anyway.
425 */
426 retries = 5;
427 do {
428 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
429 &status);
430 retries--;
431 } while (ret && retries);
432 if (ret) {
433 dev_err(dev, "failed to read status (%d)\n", ret);
434 return ret;
435 }
436
437 ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
438 SII902X_SYS_CTRL_DDC_BUS_REQ |
439 SII902X_SYS_CTRL_DDC_BUS_GRTD, 0);
440 if (ret)
441 return ret;
442
443 timeout = jiffies +
444 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
445 do {
446 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
447 &status);
448 if (ret)
449 return ret;
450 } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
451 SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
452 time_before(jiffies, timeout));
453
454 if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
455 SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
456 dev_err(dev, "failed to release the i2c bus\n");
457 return -ETIMEDOUT;
458 }
459
460 return 0;
461}
462
675605c1
BB
463static int sii902x_probe(struct i2c_client *client,
464 const struct i2c_device_id *id)
465{
466 struct device *dev = &client->dev;
467 unsigned int status = 0;
468 struct sii902x *sii902x;
469 u8 chipid[4];
470 int ret;
471
21d80840
FC
472 ret = i2c_check_functionality(client->adapter,
473 I2C_FUNC_SMBUS_BYTE_DATA);
474 if (!ret) {
475 dev_err(dev, "I2C adapter not suitable\n");
476 return -EIO;
477 }
478
675605c1
BB
479 sii902x = devm_kzalloc(dev, sizeof(*sii902x), GFP_KERNEL);
480 if (!sii902x)
481 return -ENOMEM;
482
483 sii902x->i2c = client;
484 sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config);
485 if (IS_ERR(sii902x->regmap))
486 return PTR_ERR(sii902x->regmap);
487
488 sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset",
489 GPIOD_OUT_LOW);
490 if (IS_ERR(sii902x->reset_gpio)) {
491 dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n",
492 PTR_ERR(sii902x->reset_gpio));
493 return PTR_ERR(sii902x->reset_gpio);
494 }
495
496 sii902x_reset(sii902x);
497
498 ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0);
499 if (ret)
500 return ret;
501
502 ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0),
503 &chipid, 4);
504 if (ret) {
505 dev_err(dev, "regmap_read failed %d\n", ret);
506 return ret;
507 }
508
509 if (chipid[0] != 0xb0) {
510 dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n",
511 chipid[0]);
512 return -EINVAL;
513 }
514
515 /* Clear all pending interrupts */
516 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
517 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
518
519 if (client->irq > 0) {
520 regmap_write(sii902x->regmap, SII902X_INT_ENABLE,
521 SII902X_HOTPLUG_EVENT);
522
523 ret = devm_request_threaded_irq(dev, client->irq, NULL,
524 sii902x_interrupt,
525 IRQF_ONESHOT, dev_name(dev),
526 sii902x);
527 if (ret)
528 return ret;
529 }
530
531 sii902x->bridge.funcs = &sii902x_bridge_funcs;
532 sii902x->bridge.of_node = dev->of_node;
d341a640 533 drm_bridge_add(&sii902x->bridge);
675605c1
BB
534
535 i2c_set_clientdata(client, sii902x);
536
21d80840
FC
537 sii902x->i2cmux = i2c_mux_alloc(client->adapter, dev,
538 1, 0, I2C_MUX_GATE,
539 sii902x_i2c_bypass_select,
540 sii902x_i2c_bypass_deselect);
541 if (!sii902x->i2cmux)
542 return -ENOMEM;
543
544 sii902x->i2cmux->priv = sii902x;
545 return i2c_mux_add_adapter(sii902x->i2cmux, 0, 0, 0);
675605c1
BB
546}
547
548static int sii902x_remove(struct i2c_client *client)
549
550{
551 struct sii902x *sii902x = i2c_get_clientdata(client);
552
21d80840 553 i2c_mux_del_adapters(sii902x->i2cmux);
675605c1
BB
554 drm_bridge_remove(&sii902x->bridge);
555
556 return 0;
557}
558
559static const struct of_device_id sii902x_dt_ids[] = {
560 { .compatible = "sil,sii9022", },
561 { }
562};
563MODULE_DEVICE_TABLE(of, sii902x_dt_ids);
564
565static const struct i2c_device_id sii902x_i2c_ids[] = {
566 { "sii9022", 0 },
567 { },
568};
569MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids);
570
571static struct i2c_driver sii902x_driver = {
572 .probe = sii902x_probe,
573 .remove = sii902x_remove,
574 .driver = {
575 .name = "sii902x",
576 .of_match_table = sii902x_dt_ids,
577 },
578 .id_table = sii902x_i2c_ids,
579};
580module_i2c_driver(sii902x_driver);
581
582MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
583MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
584MODULE_LICENSE("GPL");