f2b08c9d4974f413d86a8d3de904596917caf6ec
[cascardo/linux.git] / drivers / gpu / drm / bridge / anx7808.c
1 /*
2  * Copyright (C) 2013 Google, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * Based on code obtained from Analogix Inc with copyright:
14  * Copyright(c) 2012, Analogix Semiconductor. All rights reserved.
15  *
16  */
17
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_gpio.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/workqueue.h>
26 #include "drmP.h"
27 #include "anx7808regs.h"
28
29 #define ANX7808_DEVICE_ID 0x7808
30 #define AUX_WAIT_MS 100
31 #define AUX_BUFFER_SIZE 0x10
32 #define CABLE_DET_TIME_MS 1000
33
34 enum anx7808_state {
35         STATE_INIT,
36         STATE_CABLE_DETECTED,
37         STATE_HDMI_OK,
38         STATE_DP_OK,
39         STATE_PLAY,
40 };
41
42 struct anx7808_data {
43         int pd_gpio;
44         int reset_gpio;
45         int intp_gpio;
46         int cable_det_gpio;
47         int cable_det_irq;
48         struct timer_list cable_det_timer;
49         struct regulator *vdd_mydp;
50         struct i2c_client *tx_p0;
51         struct i2c_client *tx_p1;
52         struct i2c_client *tx_p2;
53         struct i2c_client *rx_p0;
54         struct i2c_client *rx_p1;
55         struct delayed_work play_video;
56         struct workqueue_struct *wq;
57 };
58
59 static struct i2c_client *anx7808_addr_to_client(struct anx7808_data *anx7808,
60                                                  uint16_t addr)
61 {
62         switch (addr >> 8) {
63         case TX_P0:
64                 return anx7808->tx_p0;
65         case TX_P1:
66                 return anx7808->tx_p1;
67         case TX_P2:
68                 return anx7808->tx_p2;
69         case RX_P0:
70                 return anx7808->rx_p0;
71         case RX_P1:
72                 return anx7808->rx_p1;
73         default:
74                 break;
75         }
76         DRM_ERROR("Invalid i2c address %04x.\n", addr);
77         return NULL;
78 }
79
80 static int anx7808_read_reg(struct anx7808_data *anx7808, uint16_t addr,
81                             uint8_t *value)
82 {
83         int ret = 0;
84         struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
85
86         if (client == NULL) {
87                 *value = 0;
88                 return -EINVAL;
89         }
90
91         ret = i2c_smbus_read_byte_data(client, addr & 0xFF);
92         if (ret < 0) {
93                 DRM_ERROR("Failed to read i2c addr=%04x.\n", addr);
94                 *value = 0;
95                 return ret;
96         }
97         *value = ret;
98         return 0;
99 }
100
101 static int anx7808_write_reg(struct anx7808_data *anx7808, uint16_t addr,
102                              uint8_t value)
103 {
104         int ret = 0;
105         struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
106
107         if (client == NULL)
108                 return -EINVAL;
109
110         ret = i2c_smbus_write_byte_data(client, addr & 0xFF, value);
111         if (ret < 0) {
112                 DRM_ERROR("Failed to write i2c addr=%04x.\n", addr);
113                 return -EIO;
114         }
115         return 0;
116 }
117
118 static int anx7808_set_bits(struct anx7808_data *anx7808, uint16_t addr,
119                             uint8_t bits)
120 {
121         uint8_t c;
122         int ret = 0;
123         ret = anx7808_read_reg(anx7808, addr, &c);
124         if (ret)
125                 return ret;
126         return anx7808_write_reg(anx7808, addr, c | bits);
127 }
128
129 static int anx7808_clear_bits(struct anx7808_data *anx7808, uint16_t addr,
130                               uint8_t bits)
131 {
132         uint8_t c;
133         int ret = 0;
134         ret = anx7808_read_reg(anx7808, addr, &c);
135         if (ret)
136                 return ret;
137         return anx7808_write_reg(anx7808, addr, c & (~bits));
138 }
139
140 static int anx7808_power_on(struct anx7808_data *anx7808)
141 {
142         int ret = 0;
143
144         DRM_INFO("Powering on ANX7808.\n");
145
146         gpio_set_value(anx7808->reset_gpio, 0);
147         usleep_range(1000, 2000);
148         gpio_set_value(anx7808->pd_gpio, 0);
149         usleep_range(2000, 4000);
150         ret = regulator_enable(anx7808->vdd_mydp);
151         if (ret < 0) {
152                 DRM_ERROR("Failed to power on ANX7808: %d", ret);
153                 return ret;
154         }
155         msleep(20);
156         gpio_set_value(anx7808->reset_gpio, 1);
157         return 0;
158 }
159
160 static int anx7808_power_off(struct anx7808_data *anx7808)
161 {
162         int ret = 0;
163
164         DRM_INFO("Powering off ANX7808.\n");
165
166         gpio_set_value(anx7808->reset_gpio, 0);
167         usleep_range(1000, 2000);
168         ret = regulator_disable(anx7808->vdd_mydp);
169         if (ret < 0) {
170                 DRM_ERROR("Failed to power off ANX7808: %d", ret);
171                 return ret;
172         }
173         usleep_range(5000, 10000);
174         gpio_set_value(anx7808->pd_gpio, 1);
175         usleep_range(1000, 2000);
176         return ret;
177 }
178
179 static int anx7808_chip_located(struct anx7808_data *anx7808)
180 {
181         int ret = 0;
182         uint16_t id;
183         uint8_t idh = 0, idl = 0;
184         ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDL_REG, &idl);
185         ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDH_REG, &idh);
186         if (ret)
187                 return ret;
188         id = idl | (idh << 8);
189         if (id != ANX7808_DEVICE_ID) {
190                 DRM_ERROR("ANX7808 not found.  ID reg contains: %04x\n", id);
191                 return -ENODEV;
192         }
193         DRM_INFO("ANX7808 found.\n");
194         return 0;
195 }
196
197 static int anx7808_vbus_power_on(struct anx7808_data *anx7808)
198 {
199         uint8_t status;
200
201         anx7808_set_bits(anx7808, SP_TX_PLL_FILTER_CTRL6,
202                          P5V_PROTECT_PD | SHORT_PROTECT_PD);
203         anx7808_clear_bits(anx7808, SP_TX_PLL_FILTER_CTRL11, V33_SWITCH_ON);
204         anx7808_set_bits(anx7808, SP_TX_PLL_FILTER_CTRL11, V33_SWITCH_ON);
205
206         anx7808_read_reg(anx7808, SP_TX_PLL_FILTER_CTRL6, &status);
207         if (status & (P5V_PROTECT | SHORT_PROTECT)) {
208                 DRM_ERROR("Failed to enable VBUS: 0x%02x.\n", status);
209                 return -EIO;
210         }
211         DRM_DEBUG_KMS("Enabled VBUS.\n");
212         return 0;
213 }
214
215 static int anx7808_aux_wait(struct anx7808_data *anx7808)
216 {
217         int err;
218         uint8_t status;
219         unsigned long start = jiffies;
220
221         while ((jiffies - start) <= msecs_to_jiffies(AUX_WAIT_MS)) {
222                 err = anx7808_read_reg(anx7808, SP_TX_AUX_STATUS, &status);
223                 if (err)
224                         return err;
225                 if (!(status & AUX_BUSY))
226                         break;
227                 usleep_range(100, 200);
228         }
229
230         if (status) {
231                 DRM_ERROR("Failed to read AUX channel: 0x%02x\n", status);
232                 return -EIO;
233         }
234         return 0;
235 }
236
237 static int anx7808_aux_read(struct anx7808_data *anx7808, uint32_t addr,
238                             uint8_t cmd, uint8_t count, uint8_t *pBuf)
239 {
240         int i;
241         int err = 0;
242         int addrl = (addr >> 0) & 0xFF;
243         int addrm = (addr >> 8) & 0xFF;
244         int addrh = (addr >> 16) & 0x0F;
245
246         if (count > AUX_BUFFER_SIZE)
247                 return -EINVAL;
248
249         err |= anx7808_write_reg(anx7808, SP_TX_BUF_DATA_COUNT_REG, BUF_CLR);
250         err |= anx7808_write_reg(anx7808, SP_TX_AUX_CTRL_REG,
251                                  ((count - 1) << 4) | cmd);
252         err |= anx7808_write_reg(anx7808, SP_TX_AUX_ADDR_7_0_REG, addrl);
253         err |= anx7808_write_reg(anx7808, SP_TX_AUX_ADDR_15_8_REG, addrm);
254         err |= anx7808_write_reg(anx7808, SP_TX_AUX_ADDR_19_16_REG, addrh);
255         err |= anx7808_set_bits(anx7808, SP_TX_AUX_CTRL_REG2, AUX_OP_EN);
256         if (err)
257                 return -EIO;
258         usleep_range(2000, 4000);
259
260         err = anx7808_aux_wait(anx7808);
261         if (err)
262                 return err;
263
264         for (i = 0; i < count; i++)
265                 err |= anx7808_read_reg(anx7808, SP_TX_BUF_DATA_0_REG + i,
266                                         pBuf + i);
267         if (err)
268                 return -EIO;
269
270         return 0;
271 }
272
273 static int anx7808_aux_dpcd_read(struct anx7808_data *anx7808, uint32_t addr,
274                                  uint8_t count, uint8_t *pBuf)
275 {
276         return anx7808_aux_read(anx7808, addr, AUX_DPCD, count, pBuf);
277 }
278
279 static void anx7808_rx_initialization(struct anx7808_data *anx7808)
280 {
281         DRM_DEBUG_KMS("Initializing ANX7808 receiver.\n");
282
283         anx7808_write_reg(anx7808, HDMI_RX_HDMI_MUTE_CTRL_REG,
284                           AUD_MUTE | VID_MUTE);
285         anx7808_set_bits(anx7808, HDMI_RX_CHIP_CTRL_REG,
286                          MAN_HDMI5V_DET | PLLLOCK_CKDT_EN | DIGITAL_CKDT_EN);
287         anx7808_set_bits(anx7808, HDMI_RX_AEC_CTRL_REG, AVC_OE);
288         anx7808_set_bits(anx7808, HDMI_RX_SRST_REG, HDCP_MAN_RST);
289         usleep_range(1000, 2000);
290         anx7808_clear_bits(anx7808, HDMI_RX_SRST_REG, HDCP_MAN_RST);
291         anx7808_set_bits(anx7808, HDMI_RX_SRST_REG, SW_MAN_RST);
292         usleep_range(1000, 2000);
293         anx7808_clear_bits(anx7808, HDMI_RX_SRST_REG, SW_MAN_RST);
294         anx7808_set_bits(anx7808, HDMI_RX_SRST_REG, TMDS_RST);
295         anx7808_write_reg(anx7808, HDMI_RX_AEC_EN0_REG,
296                           AEC_EN07 | AEC_EN06 | AEC_EN05 | AEC_EN02);
297         anx7808_write_reg(anx7808, HDMI_RX_AEC_EN1_REG,
298                           AEC_EN12 | AEC_EN10 | AEC_EN09 | AEC_EN08);
299         anx7808_write_reg(anx7808, HDMI_RX_AEC_EN2_REG,
300                           AEC_EN23 | AEC_EN22 | AEC_EN21 | AEC_EN20);
301         anx7808_set_bits(anx7808, HDMI_RX_AEC_CTRL_REG, AVC_EN);
302         anx7808_clear_bits(anx7808, HDMI_RX_SYS_PWDN1_REG, PWDN_CTRL);
303
304         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK1_REG, 0x00);
305         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK2_REG, 0x00);
306         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK3_REG, 0x00);
307         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK4_REG, 0x00);
308         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK5_REG, 0x00);
309         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK6_REG, 0x00);
310         anx7808_write_reg(anx7808, HDMI_RX_INT_MASK7_REG, 0x00);
311
312         anx7808_set_bits(anx7808, HDMI_RX_VID_DATA_RNG_CTRL_REG,
313                          R2Y_INPUT_LIMIT);
314         anx7808_write_reg(anx7808, HDMI_RX_CEC_CTRL_REG, CEC_RST);
315         anx7808_write_reg(anx7808, HDMI_RX_CEC_SPEED_CTRL_REG, CEC_SPEED_27M);
316         anx7808_write_reg(anx7808, HDMI_RX_CEC_CTRL_REG, CEC_RX_EN);
317
318         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG2, 0x00);
319         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG4, 0x28);
320         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG5, 0xE3);
321         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG7, 0x70);
322         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG19, 0x00);
323         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG21, 0x04);
324         anx7808_write_reg(anx7808, HDMI_RX_TMDS_CTRL_REG22, 0x38);
325
326         DRM_DEBUG_KMS("Issuing HPD low.\n");
327         anx7808_clear_bits(anx7808, SP_TX_VID_CTRL3_REG, HPD_OUT);
328         anx7808_set_bits(anx7808, HDMI_RX_TMDS_CTRL_REG6, TERM_PD);
329 }
330
331 static void anx7808_tx_initialization(struct anx7808_data *anx7808)
332 {
333         DRM_DEBUG_KMS("Initializing ANX7808 transmitter.\n");
334
335         anx7808_write_reg(anx7808, SP_TX_EXTRA_ADDR_REG, I2C_EXTRA_ADDR);
336         anx7808_set_bits(anx7808, SP_TX_HDCP_CTRL, LINK_POLLING);
337         anx7808_clear_bits(anx7808, SP_TX_HDCP_CTRL, AUTO_START | AUTO_EN);
338         anx7808_set_bits(anx7808, SP_TX_LINK_DEBUG_REG, M_VID_DEBUG);
339         anx7808_set_bits(anx7808, SP_TX_DEBUG_REG1,
340                          FORCE_HPD | FORCE_PLL_LOCK | POLLING_EN);
341         anx7808_set_bits(anx7808, SP_TX_PLL_FILTER_CTRL11, AUX_TERM_50OHM);
342         anx7808_clear_bits(anx7808, SP_TX_PLL_FILTER_CTRL6,
343                            P5V_PROTECT_PD | SHORT_PROTECT_PD);
344         anx7808_set_bits(anx7808, SP_TX_ANALOG_DEBUG_REG2, POWERON_TIME_1P5MS);
345         anx7808_set_bits(anx7808, SP_TX_HDCP_CTRL0_REG,
346                          BKSV_SRM_PASS | KSVLIST_VLD);
347
348         anx7808_write_reg(anx7808, SP_TX_ANALOG_CTRL, 0xC5);
349         anx7808_write_reg(anx7808, I2C_GEN_10US_TIMER0, 0x0E);
350         anx7808_write_reg(anx7808, I2C_GEN_10US_TIMER1, 0x01);
351
352         anx7808_write_reg(anx7808, SP_TX_DP_POLLING_CTRL_REG,
353                           AUTO_POLLING_DISABLE);
354         anx7808_write_reg(anx7808, SP_TX_LINK_CHK_TIMER, 0x1D);
355
356         anx7808_set_bits(anx7808, SP_TX_MISC_CTRL_REG, EQ_TRAINING_LOOP);
357
358         anx7808_write_reg(anx7808, SP_COMMON_INT_MASK1, 0x00);
359         anx7808_write_reg(anx7808, SP_COMMON_INT_MASK2, 0x00);
360         anx7808_write_reg(anx7808, SP_COMMON_INT_MASK3, 0x00);
361         anx7808_write_reg(anx7808, SP_COMMON_INT_MASK4, 0x00);
362         anx7808_write_reg(anx7808, SP_INT_MASK, 0x90);
363         anx7808_write_reg(anx7808, SP_TX_INT_CTRL_REG, 0x01);
364
365         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG0, 0x19);
366         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG4, 0x1B);
367         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG7, 0x22);
368         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG9, 0x23);
369         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG14, 0x09);
370         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG17, 0x16);
371         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG19, 0x1F);
372         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG1, 0x26);
373         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG5, 0x28);
374         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG8, 0x2F);
375         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG15, 0x10);
376         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG18, 0x1F);
377         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG2, 0x36);
378         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG6, 0x3C);
379         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG16, 0x18);
380         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG3, 0x3F);
381 }
382
383 static void anx7808_init_pipeline(struct anx7808_data *anx7808)
384 {
385         anx7808_rx_initialization(anx7808);
386         anx7808_tx_initialization(anx7808);
387
388         anx7808_vbus_power_on(anx7808);
389         msleep(20);
390
391         DRM_DEBUG_KMS("Issuing HPD high.\n");
392         anx7808_set_bits(anx7808, SP_TX_VID_CTRL3_REG, HPD_OUT);
393         anx7808_clear_bits(anx7808, HDMI_RX_TMDS_CTRL_REG6, TERM_PD);
394 }
395
396 static int anx7808_detect_hdmi_input(struct anx7808_data *anx7808)
397 {
398         uint8_t sys_status;
399
400         anx7808_read_reg(anx7808, HDMI_RX_SYS_STATUS_REG, &sys_status);
401         if (!(sys_status & TMDS_CLOCK_DET)) {
402                 DRM_INFO("Waiting for HDMI clock.\n");
403                 return -EAGAIN;
404         }
405         if (!(sys_status & TMDS_DE_DET)) {
406                 DRM_INFO("Waiting for HDMI signal.\n");
407                 return -EAGAIN;
408         }
409
410         anx7808_write_reg(anx7808, HDMI_RX_HDMI_MUTE_CTRL_REG, 0x00);
411
412         return 0;
413 }
414
415 static int anx7808_dp_link_training(struct anx7808_data *anx7808)
416 {
417         uint8_t dp_bw, status;
418         int err;
419
420         err = anx7808_aux_dpcd_read(anx7808, MAX_LINK_RATE, 1, &dp_bw);
421         if (err)
422                 return err;
423         DRM_DEBUG_KMS("DP link bandwidth: 0x%02x\n", dp_bw);
424
425         anx7808_clear_bits(anx7808, SP_POWERD_CTRL_REG, VIDEO_PD);
426         anx7808_set_bits(anx7808, SP_TX_VID_CTRL1_REG, VIDEO_EN | VIDEO_MUTE);
427
428         msleep(20);
429
430         anx7808_read_reg(anx7808, SP_TX_SYS_CTRL2_REG, &status);
431         anx7808_write_reg(anx7808, SP_TX_SYS_CTRL2_REG, status);
432         anx7808_read_reg(anx7808, SP_TX_SYS_CTRL2_REG, &status);
433         if (status & CHA_STA) {
434                 DRM_INFO("Waiting for DP clock: 0x%02x\n", status);
435                 return -EAGAIN;
436         }
437
438         anx7808_set_bits(anx7808, SP_TX_SYS_CTRL3_REG, STRM_VALID);
439         anx7808_read_reg(anx7808, SP_TX_SYS_CTRL3_REG, &status);
440         if (!(status & STRM_VALID)) {
441                 DRM_INFO("Waiting for DP signal: 0x%02x\n", status);
442                 return -EAGAIN;
443         }
444
445         anx7808_clear_bits(anx7808, SP_TX_VID_CTRL1_REG, VIDEO_EN);
446
447         anx7808_set_bits(anx7808, SP_TX_ANALOG_PD_REG, CH0_PD);
448         usleep_range(1000, 2000);
449         anx7808_clear_bits(anx7808, SP_TX_ANALOG_PD_REG, CH0_PD);
450
451         anx7808_set_bits(anx7808, SP_TX_PLL_CTRL_REG, PLL_RST);
452         usleep_range(1000, 2000);
453         anx7808_clear_bits(anx7808, SP_TX_PLL_CTRL_REG, PLL_RST);
454
455         anx7808_write_reg(anx7808, SP_TX_LINK_BW_SET_REG, 0x14);
456         anx7808_write_reg(anx7808, SP_TX_LT_CTRL_REG, SP_TX_LT_EN);
457
458         anx7808_read_reg(anx7808, SP_TX_LT_CTRL_REG, &status);
459         if (status & 0x70) {
460                 DRM_INFO("Waiting for DP link training: 0x%02x\n", status);
461                 return -EAGAIN;
462         }
463
464         anx7808_clear_bits(anx7808, SP_TX_VID_CTRL1_REG, VIDEO_MUTE);
465
466         return 0;
467 }
468
469 static void anx7808_config_dp_output(struct anx7808_data *anx7808)
470 {
471         anx7808_set_bits(anx7808, SP_TX_VID_CTRL1_REG, VIDEO_EN);
472 }
473
474 static int anx7808_check_polling_err(struct anx7808_data *anx7808)
475 {
476         uint8_t tx_int_status;
477         anx7808_read_reg(anx7808, SP_TX_INT_STATUS1, &tx_int_status);
478         anx7808_write_reg(anx7808, SP_TX_INT_STATUS1, tx_int_status);
479         if (tx_int_status & POLLING_ERR)
480                 return -EFAULT;
481         return 0;
482 }
483
484 static void anx7808_play_video(struct work_struct *work)
485 {
486         struct delayed_work *dw =
487                 container_of(work, struct delayed_work, work);
488         struct anx7808_data *anx7808 =
489                 container_of(dw, struct anx7808_data, play_video);
490         int ret;
491         enum anx7808_state state = STATE_CABLE_DETECTED;
492
493         ret = anx7808_power_on(anx7808);
494         if (ret)
495                 return;
496
497         anx7808_clear_bits(anx7808, SP_POWERD_CTRL_REG, REGISTER_PD);
498         anx7808_clear_bits(anx7808, SP_POWERD_CTRL_REG, TOTAL_PD);
499         anx7808_init_pipeline(anx7808);
500
501         while (state != STATE_INIT) {
502                 DRM_DEBUG_KMS("State: %d\n", state);
503
504                 /* Make progress towards playback state. */
505                 switch (state) {
506                 case STATE_CABLE_DETECTED:
507                         ret = anx7808_detect_hdmi_input(anx7808);
508                         if (ret)
509                                 break;
510                         state = STATE_HDMI_OK;
511
512                 case STATE_HDMI_OK:
513                         ret = anx7808_dp_link_training(anx7808);
514                         if (ret)
515                                 break;
516                         state = STATE_DP_OK;
517
518                 case STATE_DP_OK:
519                         anx7808_config_dp_output(anx7808);
520                         state = STATE_PLAY;
521                 default:
522                         break;
523                 }
524
525                 /* Check for failures */
526                 if (state == STATE_PLAY) {
527                         ret = anx7808_check_polling_err(anx7808);
528                         if (ret) {
529                                 DRM_INFO("Polling error: %02x", ret);
530                                 state = STATE_INIT;
531                         }
532                 }
533
534                 msleep(300);
535         }
536
537         anx7808_power_off(anx7808);
538 }
539
540 static void anx7808_cable_det_timer(unsigned long data)
541 {
542         struct anx7808_data *anx7808 = (struct anx7808_data *)data;
543         DRM_INFO("anx7808_cable_det_timer");
544         queue_delayed_work(anx7808->wq, &anx7808->play_video, 0);
545 }
546
547 static irqreturn_t anx7808_cable_det_isr(int irq, void *data)
548 {
549         struct anx7808_data *anx7808 = data;
550         if (gpio_get_value(anx7808->cable_det_gpio)) {
551                 DRM_INFO("Cable detected; Setting cable_det_timer.\n");
552                 mod_timer(&anx7808->cable_det_timer,
553                           jiffies + msecs_to_jiffies(CABLE_DET_TIME_MS));
554         } else {
555                 DRM_INFO("Cable unplugged; Deleting cable_det_timer.\n");
556                 del_timer(&anx7808->cable_det_timer);
557         }
558         return IRQ_HANDLED;
559 }
560
561 static void anx7808_free_gpios(struct anx7808_data *anx7808)
562 {
563         gpio_free(anx7808->cable_det_gpio);
564         gpio_free(anx7808->intp_gpio);
565         gpio_free(anx7808->reset_gpio);
566         gpio_free(anx7808->pd_gpio);
567 }
568
569 static void unregister_i2c_clients(struct anx7808_data *anx7808)
570 {
571         if (anx7808->rx_p1)
572                 i2c_unregister_device(anx7808->rx_p1);
573         if (anx7808->rx_p0)
574                 i2c_unregister_device(anx7808->rx_p0);
575         if (anx7808->tx_p2)
576                 i2c_unregister_device(anx7808->tx_p2);
577         if (anx7808->tx_p1)
578                 i2c_unregister_device(anx7808->tx_p1);
579 }
580
581 static int anx7808_probe(struct i2c_client *client,
582                         const struct i2c_device_id *id)
583 {
584         int ret = 0;
585         struct device_node *node = client->dev.of_node;
586         struct anx7808_data *anx7808;
587
588         anx7808 = devm_kzalloc(&client->dev, sizeof(struct anx7808_data),
589                                GFP_KERNEL);
590         if (!anx7808) {
591                 DRM_ERROR("Failed to allocate platform_data.\n");
592                 return -ENOMEM;
593         }
594         i2c_set_clientdata(client, anx7808);
595
596         INIT_DELAYED_WORK(&anx7808->play_video, anx7808_play_video);
597
598         setup_timer(&anx7808->cable_det_timer, anx7808_cable_det_timer,
599                     (unsigned long)anx7808);
600
601         anx7808->vdd_mydp = regulator_get(&client->dev, "vdd_mydp");
602         if (IS_ERR(anx7808->vdd_mydp)) {
603                 DRM_ERROR("Failed to find regulator vdd_mydp.\n");
604                 return PTR_ERR(anx7808->vdd_mydp);
605         }
606
607         anx7808->pd_gpio = of_get_named_gpio(node, "pd-gpio", 0);
608         if (!gpio_is_valid(anx7808->pd_gpio)) {
609                 DRM_ERROR("Failed to locate pd-gpio.\n");
610                 ret = anx7808->pd_gpio;
611                 goto err_reg;
612         }
613
614         anx7808->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
615         if (!gpio_is_valid(anx7808->reset_gpio)) {
616                 DRM_ERROR("Failed to locate reset-gpio.\n");
617                 ret = anx7808->reset_gpio;
618                 goto err_reg;
619         }
620
621         anx7808->intp_gpio = of_get_named_gpio(node, "intp-gpio", 0);
622         if (!gpio_is_valid(anx7808->intp_gpio)) {
623                 DRM_ERROR("Failed to locate intp-gpio.\n");
624                 ret = anx7808->intp_gpio;
625                 goto err_reg;
626         }
627
628         anx7808->cable_det_gpio = of_get_named_gpio(node, "cable-det-gpio", 0);
629         if (!gpio_is_valid(anx7808->cable_det_gpio)) {
630                 DRM_ERROR("Failed to locate cable-det-gpio.\n");
631                 ret = anx7808->cable_det_gpio;
632                 goto err_reg;
633         }
634
635         ret = gpio_request_one(anx7808->pd_gpio, GPIOF_OUT_INIT_HIGH,
636                                "anx7808_pd_gpio");
637         if (ret) {
638                 DRM_ERROR("Failed to request pd_gpio.\n");
639                 goto err_gpio;
640         }
641
642         ret = gpio_request_one(anx7808->reset_gpio, GPIOF_OUT_INIT_LOW,
643                                "anx7808_reset_gpio");
644         if (ret) {
645                 DRM_ERROR("Failed to request reset_gpio.\n");
646                 goto err_gpio;
647         }
648
649         ret = gpio_request_one(anx7808->intp_gpio, GPIOF_DIR_IN,
650                                "anx7808_intp_gpio");
651         if (ret) {
652                 DRM_ERROR("Failed to request intp_gpio.\n");
653                 goto err_gpio;
654         }
655
656         ret = gpio_request_one(anx7808->cable_det_gpio, GPIOF_DIR_IN,
657                                "anx7808_cable_det_gpio");
658         if (ret) {
659                 DRM_ERROR("Failed to request cable_det_gpio.\n");
660                 goto err_gpio;
661         }
662
663         anx7808->tx_p0 = client;
664         if (!((anx7808->tx_p0->addr) == TX_P0 >> 1)) {
665                 DRM_ERROR("I2C client address %02x != expected value %02x.\n",
666                           anx7808->tx_p0->addr, TX_P0 >> 1);
667                 goto err_i2c;
668         }
669         anx7808->tx_p1 = i2c_new_dummy(client->adapter, TX_P1 >> 1);
670         if (!anx7808->tx_p1) {
671                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P1 >> 1);
672                 ret = -EINVAL;
673                 goto err_i2c;
674         }
675         anx7808->tx_p2 = i2c_new_dummy(client->adapter, TX_P2 >> 1);
676         if (!anx7808->tx_p2) {
677                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P2 >> 1);
678                 ret = -EINVAL;
679                 goto err_i2c;
680         }
681         anx7808->rx_p0 = i2c_new_dummy(client->adapter, RX_P0 >> 1);
682         if (!anx7808->rx_p0) {
683                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P0 >> 1);
684                 ret = -EINVAL;
685                 goto err_i2c;
686         }
687         anx7808->rx_p1 = i2c_new_dummy(client->adapter, RX_P1 >> 1);
688         if (!anx7808->rx_p1) {
689                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P1 >> 1);
690                 ret = -EINVAL;
691                 goto err_i2c;
692         }
693
694         anx7808_power_on(anx7808);
695         ret = anx7808_chip_located(anx7808);
696         anx7808_power_off(anx7808);
697         if (ret)
698                 goto err_i2c;
699
700         anx7808->cable_det_irq = gpio_to_irq(anx7808->cable_det_gpio);
701         if (anx7808->cable_det_irq < 0) {
702                 DRM_ERROR("Failed to get irq: %d\n", anx7808->cable_det_irq);
703                 goto err_i2c;
704         }
705
706         anx7808->wq = create_singlethread_workqueue("anx7808_work");
707         if (anx7808->wq == NULL) {
708                 DRM_ERROR("Failed to create work queue.\n");
709                 ret = -ENOMEM;
710                 goto err_i2c;
711         }
712
713         ret = devm_request_irq(&client->dev,
714                                anx7808->cable_det_irq,
715                                anx7808_cable_det_isr,
716                                IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
717                                "anx7808_cable_det",
718                                anx7808);
719         if (ret < 0) {
720                 DRM_ERROR("Failed to request irq: %d\n", ret);
721                 goto err_wq;
722         }
723
724         DRM_INFO("ANX7808 initialization successful.\n");
725
726         return 0;
727 err_wq:
728         destroy_workqueue(anx7808->wq);
729 err_i2c:
730         unregister_i2c_clients(anx7808);
731 err_gpio:
732         anx7808_free_gpios(anx7808);
733 err_reg:
734         regulator_put(anx7808->vdd_mydp);
735         return ret;
736 }
737
738 static int anx7808_remove(struct i2c_client *client)
739 {
740         struct anx7808_data *anx7808 = i2c_get_clientdata(client);
741         destroy_workqueue(anx7808->wq);
742         unregister_i2c_clients(anx7808);
743         anx7808_free_gpios(anx7808);
744         regulator_put(anx7808->vdd_mydp);
745         return 0;
746 }
747
748 static const struct i2c_device_id anx7808_id[] = {
749         { "anx7808", 0 },
750         { }
751 };
752
753 MODULE_DEVICE_TABLE(i2c, anx7808_id);
754
755 static struct i2c_driver anx7808_driver = {
756         .driver = {
757                 .name = "anx7808",
758                 .owner = THIS_MODULE,
759         },
760         .probe = anx7808_probe,
761         .remove = anx7808_remove,
762         .id_table = anx7808_id,
763 };
764
765 static int __init anx7808_init(void)
766 {
767         int ret = 0;
768
769         ret = i2c_add_driver(&anx7808_driver);
770         if (ret < 0)
771                 DRM_ERROR("Failed to register anx7808 i2c driver.\n");
772         return ret;
773 }
774
775 static void __exit anx7808_exit(void)
776 {
777         i2c_del_driver(&anx7808_driver);
778 }
779
780 module_init(anx7808_init);
781 module_exit(anx7808_exit);
782
783 MODULE_DESCRIPTION("ANX7808 driver");
784 MODULE_AUTHOR("Jeremy Thorpe <jeremyt@chromium.org>");
785 MODULE_LICENSE("GPL");