Linux Audio

Check our new training course

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