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