Linux Audio

Check our new training course

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