1
2
3
4
5
6
7
8#include <linux/delay.h>
9#include <linux/gpio.h>
10#include <linux/gpio/consumer.h>
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_gpio.h>
15
16#include <drm/drm_atomic_helper.h>
17#include <drm/drm_crtc.h>
18#include <drm/drm_edid.h>
19#include <drm/drm_of.h>
20#include <drm/drm_panel.h>
21#include <drm/drm_print.h>
22#include <drm/drm_probe_helper.h>
23
24#define PTN3460_EDID_ADDR 0x0
25#define PTN3460_EDID_EMULATION_ADDR 0x84
26#define PTN3460_EDID_ENABLE_EMULATION 0
27#define PTN3460_EDID_EMULATION_SELECTION 1
28#define PTN3460_EDID_SRAM_LOAD_ADDR 0x85
29
30struct ptn3460_bridge {
31 struct drm_connector connector;
32 struct i2c_client *client;
33 struct drm_bridge bridge;
34 struct edid *edid;
35 struct drm_panel *panel;
36 struct gpio_desc *gpio_pd_n;
37 struct gpio_desc *gpio_rst_n;
38 u32 edid_emulation;
39 bool enabled;
40};
41
42static inline struct ptn3460_bridge *
43 bridge_to_ptn3460(struct drm_bridge *bridge)
44{
45 return container_of(bridge, struct ptn3460_bridge, bridge);
46}
47
48static inline struct ptn3460_bridge *
49 connector_to_ptn3460(struct drm_connector *connector)
50{
51 return container_of(connector, struct ptn3460_bridge, connector);
52}
53
54static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
55 u8 *buf, int len)
56{
57 int ret;
58
59 ret = i2c_master_send(ptn_bridge->client, &addr, 1);
60 if (ret <= 0) {
61 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
62 return ret;
63 }
64
65 ret = i2c_master_recv(ptn_bridge->client, buf, len);
66 if (ret <= 0) {
67 DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
68 return ret;
69 }
70
71 return 0;
72}
73
74static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
75 char val)
76{
77 int ret;
78 char buf[2];
79
80 buf[0] = addr;
81 buf[1] = val;
82
83 ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
84 if (ret <= 0) {
85 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
86 return ret;
87 }
88
89 return 0;
90}
91
92static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
93{
94 int ret;
95 char val;
96
97
98 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
99 ptn_bridge->edid_emulation);
100 if (ret) {
101 DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
102 return ret;
103 }
104
105
106 val = 1 << PTN3460_EDID_ENABLE_EMULATION |
107 ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;
108
109 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
110 if (ret) {
111 DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
112 return ret;
113 }
114
115 return 0;
116}
117
118static void ptn3460_pre_enable(struct drm_bridge *bridge)
119{
120 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
121 int ret;
122
123 if (ptn_bridge->enabled)
124 return;
125
126 gpiod_set_value(ptn_bridge->gpio_pd_n, 1);
127
128 gpiod_set_value(ptn_bridge->gpio_rst_n, 0);
129 usleep_range(10, 20);
130 gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
131
132 if (drm_panel_prepare(ptn_bridge->panel)) {
133 DRM_ERROR("failed to prepare panel\n");
134 return;
135 }
136
137
138
139
140
141
142 msleep(90);
143
144 ret = ptn3460_select_edid(ptn_bridge);
145 if (ret)
146 DRM_ERROR("Select EDID failed ret=%d\n", ret);
147
148 ptn_bridge->enabled = true;
149}
150
151static void ptn3460_enable(struct drm_bridge *bridge)
152{
153 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
154
155 if (drm_panel_enable(ptn_bridge->panel)) {
156 DRM_ERROR("failed to enable panel\n");
157 return;
158 }
159}
160
161static void ptn3460_disable(struct drm_bridge *bridge)
162{
163 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
164
165 if (!ptn_bridge->enabled)
166 return;
167
168 ptn_bridge->enabled = false;
169
170 if (drm_panel_disable(ptn_bridge->panel)) {
171 DRM_ERROR("failed to disable panel\n");
172 return;
173 }
174
175 gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
176 gpiod_set_value(ptn_bridge->gpio_pd_n, 0);
177}
178
179static void ptn3460_post_disable(struct drm_bridge *bridge)
180{
181 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
182
183 if (drm_panel_unprepare(ptn_bridge->panel)) {
184 DRM_ERROR("failed to unprepare panel\n");
185 return;
186 }
187}
188
189static int ptn3460_get_modes(struct drm_connector *connector)
190{
191 struct ptn3460_bridge *ptn_bridge;
192 u8 *edid;
193 int ret, num_modes = 0;
194 bool power_off;
195
196 ptn_bridge = connector_to_ptn3460(connector);
197
198 if (ptn_bridge->edid)
199 return drm_add_edid_modes(connector, ptn_bridge->edid);
200
201 power_off = !ptn_bridge->enabled;
202 ptn3460_pre_enable(&ptn_bridge->bridge);
203
204 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
205 if (!edid) {
206 DRM_ERROR("Failed to allocate EDID\n");
207 return 0;
208 }
209
210 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
211 EDID_LENGTH);
212 if (ret) {
213 kfree(edid);
214 goto out;
215 }
216
217 ptn_bridge->edid = (struct edid *)edid;
218 drm_connector_update_edid_property(connector, ptn_bridge->edid);
219
220 num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);
221
222out:
223 if (power_off)
224 ptn3460_disable(&ptn_bridge->bridge);
225
226 return num_modes;
227}
228
229static const struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
230 .get_modes = ptn3460_get_modes,
231};
232
233static const struct drm_connector_funcs ptn3460_connector_funcs = {
234 .fill_modes = drm_helper_probe_single_connector_modes,
235 .destroy = drm_connector_cleanup,
236 .reset = drm_atomic_helper_connector_reset,
237 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
238 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
239};
240
241static int ptn3460_bridge_attach(struct drm_bridge *bridge)
242{
243 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
244 int ret;
245
246 if (!bridge->encoder) {
247 DRM_ERROR("Parent encoder object not found");
248 return -ENODEV;
249 }
250
251 ptn_bridge->connector.polled = DRM_CONNECTOR_POLL_HPD;
252 ret = drm_connector_init(bridge->dev, &ptn_bridge->connector,
253 &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
254 if (ret) {
255 DRM_ERROR("Failed to initialize connector with drm\n");
256 return ret;
257 }
258 drm_connector_helper_add(&ptn_bridge->connector,
259 &ptn3460_connector_helper_funcs);
260 drm_connector_register(&ptn_bridge->connector);
261 drm_connector_attach_encoder(&ptn_bridge->connector,
262 bridge->encoder);
263
264 if (ptn_bridge->panel)
265 drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector);
266
267 drm_helper_hpd_irq_event(ptn_bridge->connector.dev);
268
269 return ret;
270}
271
272static const struct drm_bridge_funcs ptn3460_bridge_funcs = {
273 .pre_enable = ptn3460_pre_enable,
274 .enable = ptn3460_enable,
275 .disable = ptn3460_disable,
276 .post_disable = ptn3460_post_disable,
277 .attach = ptn3460_bridge_attach,
278};
279
280static int ptn3460_probe(struct i2c_client *client,
281 const struct i2c_device_id *id)
282{
283 struct device *dev = &client->dev;
284 struct ptn3460_bridge *ptn_bridge;
285 int ret;
286
287 ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
288 if (!ptn_bridge) {
289 return -ENOMEM;
290 }
291
292 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0, &ptn_bridge->panel, NULL);
293 if (ret)
294 return ret;
295
296 ptn_bridge->client = client;
297
298 ptn_bridge->gpio_pd_n = devm_gpiod_get(&client->dev, "powerdown",
299 GPIOD_OUT_HIGH);
300 if (IS_ERR(ptn_bridge->gpio_pd_n)) {
301 ret = PTR_ERR(ptn_bridge->gpio_pd_n);
302 dev_err(dev, "cannot get gpio_pd_n %d\n", ret);
303 return ret;
304 }
305
306
307
308
309
310 ptn_bridge->gpio_rst_n = devm_gpiod_get(&client->dev, "reset",
311 GPIOD_OUT_LOW);
312 if (IS_ERR(ptn_bridge->gpio_rst_n)) {
313 ret = PTR_ERR(ptn_bridge->gpio_rst_n);
314 DRM_ERROR("cannot get gpio_rst_n %d\n", ret);
315 return ret;
316 }
317
318 ret = of_property_read_u32(dev->of_node, "edid-emulation",
319 &ptn_bridge->edid_emulation);
320 if (ret) {
321 dev_err(dev, "Can't read EDID emulation value\n");
322 return ret;
323 }
324
325 ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
326 ptn_bridge->bridge.of_node = dev->of_node;
327 drm_bridge_add(&ptn_bridge->bridge);
328
329 i2c_set_clientdata(client, ptn_bridge);
330
331 return 0;
332}
333
334static int ptn3460_remove(struct i2c_client *client)
335{
336 struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
337
338 drm_bridge_remove(&ptn_bridge->bridge);
339
340 return 0;
341}
342
343static const struct i2c_device_id ptn3460_i2c_table[] = {
344 {"ptn3460", 0},
345 {},
346};
347MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table);
348
349static const struct of_device_id ptn3460_match[] = {
350 { .compatible = "nxp,ptn3460" },
351 {},
352};
353MODULE_DEVICE_TABLE(of, ptn3460_match);
354
355static struct i2c_driver ptn3460_driver = {
356 .id_table = ptn3460_i2c_table,
357 .probe = ptn3460_probe,
358 .remove = ptn3460_remove,
359 .driver = {
360 .name = "nxp,ptn3460",
361 .of_match_table = ptn3460_match,
362 },
363};
364module_i2c_driver(ptn3460_driver);
365
366MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>");
367MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver");
368MODULE_LICENSE("GPL v2");
369