1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/backlight.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/gpio.h>
20#include <linux/gpio/consumer.h>
21#include <linux/i2c.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/pm.h>
26#include <linux/regulator/consumer.h>
27#include <drm/drm_atomic_helper.h>
28#include <drm/drm_crtc.h>
29#include <drm/drm_crtc_helper.h>
30#include <drm/drm_of.h>
31#include <drm/drm_panel.h>
32#include <drm/drmP.h>
33
34
35#define PS8622_MAX_BRIGHTNESS 0xff
36
37
38#define PS8622_POWER_RISE_T1_MIN_US 10
39#define PS8622_POWER_RISE_T1_MAX_US 10000
40#define PS8622_RST_HIGH_T2_MIN_US 3000
41#define PS8622_RST_HIGH_T2_MAX_US 30000
42#define PS8622_PWMO_END_T12_MS 200
43#define PS8622_POWER_FALL_T16_MAX_US 10000
44#define PS8622_POWER_OFF_T17_MS 500
45
46#if ((PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US) > \
47 (PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US))
48#error "T2.min + T1.max must be less than T2.max + T1.min"
49#endif
50
51struct ps8622_bridge {
52 struct drm_connector connector;
53 struct i2c_client *client;
54 struct drm_bridge bridge;
55 struct drm_panel *panel;
56 struct regulator *v12;
57 struct backlight_device *bl;
58
59 struct gpio_desc *gpio_slp;
60 struct gpio_desc *gpio_rst;
61
62 u32 max_lane_count;
63 u32 lane_count;
64
65 bool enabled;
66};
67
68static inline struct ps8622_bridge *
69 bridge_to_ps8622(struct drm_bridge *bridge)
70{
71 return container_of(bridge, struct ps8622_bridge, bridge);
72}
73
74static inline struct ps8622_bridge *
75 connector_to_ps8622(struct drm_connector *connector)
76{
77 return container_of(connector, struct ps8622_bridge, connector);
78}
79
80static int ps8622_set(struct i2c_client *client, u8 page, u8 reg, u8 val)
81{
82 int ret;
83 struct i2c_adapter *adap = client->adapter;
84 struct i2c_msg msg;
85 u8 data[] = {reg, val};
86
87 msg.addr = client->addr + page;
88 msg.flags = 0;
89 msg.len = sizeof(data);
90 msg.buf = data;
91
92 ret = i2c_transfer(adap, &msg, 1);
93 if (ret != 1)
94 pr_warn("PS8622 I2C write (0x%02x,0x%02x,0x%02x) failed: %d\n",
95 client->addr + page, reg, val, ret);
96 return !(ret == 1);
97}
98
99static int ps8622_send_config(struct ps8622_bridge *ps8622)
100{
101 struct i2c_client *cl = ps8622->client;
102 int err = 0;
103
104
105 err = ps8622_set(cl, 0x02, 0xa1, 0x01);
106 if (err)
107 goto error;
108
109
110 err = ps8622_set(cl, 0x04, 0x14, 0x01);
111 if (err)
112 goto error;
113
114
115 err = ps8622_set(cl, 0x04, 0xe3, 0x20);
116 if (err)
117 goto error;
118
119
120 err = ps8622_set(cl, 0x04, 0xe2, 0x80);
121 if (err)
122 goto error;
123
124
125
126
127
128 err = ps8622_set(cl, 0x04, 0x8a, 0x0c);
129 if (err)
130 goto error;
131
132
133 err = ps8622_set(cl, 0x04, 0x89, 0x08);
134 if (err)
135 goto error;
136
137
138 err = ps8622_set(cl, 0x04, 0x71, 0x2d);
139 if (err)
140 goto error;
141
142
143 err = ps8622_set(cl, 0x04, 0x7d, 0x07);
144 if (err)
145 goto error;
146
147
148 err = ps8622_set(cl, 0x04, 0x7b, 0x00);
149 if (err)
150 goto error;
151
152
153 err = ps8622_set(cl, 0x04, 0x7a, 0xfd);
154 if (err)
155 goto error;
156
157
158 err = ps8622_set(cl, 0x04, 0xc0, 0x12);
159 if (err)
160 goto error;
161
162
163 err = ps8622_set(cl, 0x04, 0xc1, 0x92);
164 if (err)
165 goto error;
166
167
168 err = ps8622_set(cl, 0x04, 0xc2, 0x1c);
169 if (err)
170 goto error;
171
172
173 err = ps8622_set(cl, 0x04, 0x32, 0x80);
174 if (err)
175 goto error;
176
177
178 err = ps8622_set(cl, 0x04, 0x00, 0xb0);
179 if (err)
180 goto error;
181
182
183 err = ps8622_set(cl, 0x04, 0x15, 0x40);
184 if (err)
185 goto error;
186
187
188 err = ps8622_set(cl, 0x04, 0x54, 0x10);
189 if (err)
190 goto error;
191
192
193
194 err = ps8622_set(cl, 0x01, 0x02, 0x80 | ps8622->max_lane_count);
195 if (err)
196 goto error;
197
198
199 err = ps8622_set(cl, 0x01, 0x21, 0x80 | ps8622->lane_count);
200 if (err)
201 goto error;
202
203 err = ps8622_set(cl, 0x00, 0x52, 0x20);
204 if (err)
205 goto error;
206
207
208 err = ps8622_set(cl, 0x00, 0xf1, 0x03);
209 if (err)
210 goto error;
211
212 err = ps8622_set(cl, 0x00, 0x62, 0x41);
213 if (err)
214 goto error;
215
216
217 err = ps8622_set(cl, 0x00, 0xf6, 0x01);
218 if (err)
219 goto error;
220
221
222 err = ps8622_set(cl, 0x00, 0x77, 0x06);
223 if (err)
224 goto error;
225
226
227 err = ps8622_set(cl, 0x00, 0x4c, 0x04);
228 if (err)
229 goto error;
230
231
232 err = ps8622_set(cl, 0x01, 0xc0, 0x00);
233 if (err)
234 goto error;
235
236
237 err = ps8622_set(cl, 0x01, 0xc1, 0x1c);
238 if (err)
239 goto error;
240
241
242 err = ps8622_set(cl, 0x01, 0xc2, 0xf8);
243 if (err)
244 goto error;
245
246
247 err = ps8622_set(cl, 0x01, 0xc3, 0x44);
248 if (err)
249 goto error;
250
251
252 err = ps8622_set(cl, 0x01, 0xc4, 0x32);
253 if (err)
254 goto error;
255
256
257 err = ps8622_set(cl, 0x01, 0xc5, 0x53);
258 if (err)
259 goto error;
260
261
262 err = ps8622_set(cl, 0x01, 0xc6, 0x4c);
263 if (err)
264 goto error;
265
266
267 err = ps8622_set(cl, 0x01, 0xc7, 0x56);
268 if (err)
269 goto error;
270
271
272 err = ps8622_set(cl, 0x01, 0xc8, 0x35);
273 if (err)
274 goto error;
275
276
277 err = ps8622_set(cl, 0x01, 0xca, 0x01);
278 if (err)
279 goto error;
280
281
282 err = ps8622_set(cl, 0x01, 0xcb, 0x05);
283 if (err)
284 goto error;
285
286
287 if (ps8622->bl) {
288
289 err = ps8622_set(cl, 0x01, 0xa5, 0xa0);
290 if (err)
291 goto error;
292
293
294 err = ps8622_set(cl, 0x01, 0xa7,
295 ps8622->bl->props.brightness);
296 if (err)
297 goto error;
298 } else {
299
300 err = ps8622_set(cl, 0x01, 0xa5, 0x80);
301 if (err)
302 goto error;
303 }
304
305
306 err = ps8622_set(cl, 0x01, 0xcc, 0x13);
307 if (err)
308 goto error;
309
310
311 err = ps8622_set(cl, 0x02, 0xb1, 0x20);
312 if (err)
313 goto error;
314
315
316 err = ps8622_set(cl, 0x04, 0x10, 0x16);
317 if (err)
318 goto error;
319
320
321
322 err = ps8622_set(cl, 0x04, 0x59, 0x60);
323 if (err)
324 goto error;
325
326
327 err = ps8622_set(cl, 0x04, 0x54, 0x14);
328 if (err)
329 goto error;
330
331
332 err = ps8622_set(cl, 0x02, 0xa1, 0x91);
333
334error:
335 return err ? -EIO : 0;
336}
337
338static int ps8622_backlight_update(struct backlight_device *bl)
339{
340 struct ps8622_bridge *ps8622 = dev_get_drvdata(&bl->dev);
341 int ret, brightness = bl->props.brightness;
342
343 if (bl->props.power != FB_BLANK_UNBLANK ||
344 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
345 brightness = 0;
346
347 if (!ps8622->enabled)
348 return -EINVAL;
349
350 ret = ps8622_set(ps8622->client, 0x01, 0xa7, brightness);
351
352 return ret;
353}
354
355static const struct backlight_ops ps8622_backlight_ops = {
356 .update_status = ps8622_backlight_update,
357};
358
359static void ps8622_pre_enable(struct drm_bridge *bridge)
360{
361 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
362 int ret;
363
364 if (ps8622->enabled)
365 return;
366
367 gpiod_set_value(ps8622->gpio_rst, 0);
368
369 if (ps8622->v12) {
370 ret = regulator_enable(ps8622->v12);
371 if (ret)
372 DRM_ERROR("fails to enable ps8622->v12");
373 }
374
375 if (drm_panel_prepare(ps8622->panel)) {
376 DRM_ERROR("failed to prepare panel\n");
377 return;
378 }
379
380 gpiod_set_value(ps8622->gpio_slp, 1);
381
382
383
384
385
386
387
388
389
390
391
392 usleep_range(PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US,
393 PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US);
394
395 gpiod_set_value(ps8622->gpio_rst, 1);
396
397
398 usleep_range(20000, 30000);
399
400 ret = ps8622_send_config(ps8622);
401 if (ret) {
402 DRM_ERROR("Failed to send config to bridge (%d)\n", ret);
403 return;
404 }
405
406 ps8622->enabled = true;
407}
408
409static void ps8622_enable(struct drm_bridge *bridge)
410{
411 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
412
413 if (drm_panel_enable(ps8622->panel)) {
414 DRM_ERROR("failed to enable panel\n");
415 return;
416 }
417}
418
419static void ps8622_disable(struct drm_bridge *bridge)
420{
421 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
422
423 if (drm_panel_disable(ps8622->panel)) {
424 DRM_ERROR("failed to disable panel\n");
425 return;
426 }
427 msleep(PS8622_PWMO_END_T12_MS);
428}
429
430static void ps8622_post_disable(struct drm_bridge *bridge)
431{
432 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
433
434 if (!ps8622->enabled)
435 return;
436
437 ps8622->enabled = false;
438
439
440
441
442
443
444 gpiod_set_value(ps8622->gpio_slp, 0);
445
446 if (drm_panel_unprepare(ps8622->panel)) {
447 DRM_ERROR("failed to unprepare panel\n");
448 return;
449 }
450
451 if (ps8622->v12)
452 regulator_disable(ps8622->v12);
453
454
455
456
457
458 usleep_range(PS8622_POWER_FALL_T16_MAX_US,
459 2 * PS8622_POWER_FALL_T16_MAX_US);
460 gpiod_set_value(ps8622->gpio_rst, 0);
461
462 msleep(PS8622_POWER_OFF_T17_MS);
463}
464
465static int ps8622_get_modes(struct drm_connector *connector)
466{
467 struct ps8622_bridge *ps8622;
468
469 ps8622 = connector_to_ps8622(connector);
470
471 return drm_panel_get_modes(ps8622->panel);
472}
473
474static const struct drm_connector_helper_funcs ps8622_connector_helper_funcs = {
475 .get_modes = ps8622_get_modes,
476};
477
478static const struct drm_connector_funcs ps8622_connector_funcs = {
479 .fill_modes = drm_helper_probe_single_connector_modes,
480 .destroy = drm_connector_cleanup,
481 .reset = drm_atomic_helper_connector_reset,
482 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
483 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
484};
485
486static int ps8622_attach(struct drm_bridge *bridge)
487{
488 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
489 int ret;
490
491 if (!bridge->encoder) {
492 DRM_ERROR("Parent encoder object not found");
493 return -ENODEV;
494 }
495
496 ps8622->connector.polled = DRM_CONNECTOR_POLL_HPD;
497 ret = drm_connector_init(bridge->dev, &ps8622->connector,
498 &ps8622_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
499 if (ret) {
500 DRM_ERROR("Failed to initialize connector with drm\n");
501 return ret;
502 }
503 drm_connector_helper_add(&ps8622->connector,
504 &ps8622_connector_helper_funcs);
505 drm_connector_register(&ps8622->connector);
506 drm_connector_attach_encoder(&ps8622->connector,
507 bridge->encoder);
508
509 if (ps8622->panel)
510 drm_panel_attach(ps8622->panel, &ps8622->connector);
511
512 drm_helper_hpd_irq_event(ps8622->connector.dev);
513
514 return ret;
515}
516
517static const struct drm_bridge_funcs ps8622_bridge_funcs = {
518 .pre_enable = ps8622_pre_enable,
519 .enable = ps8622_enable,
520 .disable = ps8622_disable,
521 .post_disable = ps8622_post_disable,
522 .attach = ps8622_attach,
523};
524
525static const struct of_device_id ps8622_devices[] = {
526 {.compatible = "parade,ps8622",},
527 {.compatible = "parade,ps8625",},
528 {}
529};
530MODULE_DEVICE_TABLE(of, ps8622_devices);
531
532static int ps8622_probe(struct i2c_client *client,
533 const struct i2c_device_id *id)
534{
535 struct device *dev = &client->dev;
536 struct ps8622_bridge *ps8622;
537 int ret;
538
539 ps8622 = devm_kzalloc(dev, sizeof(*ps8622), GFP_KERNEL);
540 if (!ps8622)
541 return -ENOMEM;
542
543 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0, &ps8622->panel, NULL);
544 if (ret)
545 return ret;
546
547 ps8622->client = client;
548
549 ps8622->v12 = devm_regulator_get(dev, "vdd12");
550 if (IS_ERR(ps8622->v12)) {
551 dev_info(dev, "no 1.2v regulator found for PS8622\n");
552 ps8622->v12 = NULL;
553 }
554
555 ps8622->gpio_slp = devm_gpiod_get(dev, "sleep", GPIOD_OUT_HIGH);
556 if (IS_ERR(ps8622->gpio_slp)) {
557 ret = PTR_ERR(ps8622->gpio_slp);
558 dev_err(dev, "cannot get gpio_slp %d\n", ret);
559 return ret;
560 }
561
562
563
564
565
566 ps8622->gpio_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
567 if (IS_ERR(ps8622->gpio_rst)) {
568 ret = PTR_ERR(ps8622->gpio_rst);
569 dev_err(dev, "cannot get gpio_rst %d\n", ret);
570 return ret;
571 }
572
573 ps8622->max_lane_count = id->driver_data;
574
575 if (of_property_read_u32(dev->of_node, "lane-count",
576 &ps8622->lane_count)) {
577 ps8622->lane_count = ps8622->max_lane_count;
578 } else if (ps8622->lane_count > ps8622->max_lane_count) {
579 dev_info(dev, "lane-count property is too high,"
580 "using max_lane_count\n");
581 ps8622->lane_count = ps8622->max_lane_count;
582 }
583
584 if (!of_find_property(dev->of_node, "use-external-pwm", NULL)) {
585 ps8622->bl = backlight_device_register("ps8622-backlight",
586 dev, ps8622, &ps8622_backlight_ops,
587 NULL);
588 if (IS_ERR(ps8622->bl)) {
589 DRM_ERROR("failed to register backlight\n");
590 ret = PTR_ERR(ps8622->bl);
591 ps8622->bl = NULL;
592 return ret;
593 }
594 ps8622->bl->props.max_brightness = PS8622_MAX_BRIGHTNESS;
595 ps8622->bl->props.brightness = PS8622_MAX_BRIGHTNESS;
596 }
597
598 ps8622->bridge.funcs = &ps8622_bridge_funcs;
599 ps8622->bridge.of_node = dev->of_node;
600 drm_bridge_add(&ps8622->bridge);
601
602 i2c_set_clientdata(client, ps8622);
603
604 return 0;
605}
606
607static int ps8622_remove(struct i2c_client *client)
608{
609 struct ps8622_bridge *ps8622 = i2c_get_clientdata(client);
610
611 backlight_device_unregister(ps8622->bl);
612 drm_bridge_remove(&ps8622->bridge);
613
614 return 0;
615}
616
617static const struct i2c_device_id ps8622_i2c_table[] = {
618
619 {"ps8622", 1},
620 {"ps8625", 2},
621 {},
622};
623MODULE_DEVICE_TABLE(i2c, ps8622_i2c_table);
624
625static struct i2c_driver ps8622_driver = {
626 .id_table = ps8622_i2c_table,
627 .probe = ps8622_probe,
628 .remove = ps8622_remove,
629 .driver = {
630 .name = "ps8622",
631 .of_match_table = ps8622_devices,
632 },
633};
634module_i2c_driver(ps8622_driver);
635
636MODULE_AUTHOR("Vincent Palatin <vpalatin@chromium.org>");
637MODULE_DESCRIPTION("Parade ps8622/ps8625 eDP-LVDS converter driver");
638MODULE_LICENSE("GPL v2");
639