CHROMIUM: drm/anx7808: Add Aux channel reading.
[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 "drmP.h"
26 #include "anx7808regs.h"
27
28 #define ANX7808_DEVICE_ID 0x7808
29 #define AUX_WAIT_MS 100
30 #define AUX_BUFFER_SIZE 0x10
31
32 struct anx7808_data {
33         int pd_gpio;
34         int reset_gpio;
35         int intp_gpio;
36         int cable_det_gpio;
37         int cable_det_irq;
38         struct regulator *vdd_mydp;
39         struct i2c_client *tx_p0;
40         struct i2c_client *tx_p1;
41         struct i2c_client *tx_p2;
42         struct i2c_client *rx_p0;
43         struct i2c_client *rx_p1;
44 };
45
46 static struct i2c_client *anx7808_addr_to_client(struct anx7808_data *anx7808,
47                                                  uint16_t addr)
48 {
49         switch (addr >> 8) {
50         case TX_P0:
51                 return anx7808->tx_p0;
52         case TX_P1:
53                 return anx7808->tx_p1;
54         case TX_P2:
55                 return anx7808->tx_p2;
56         case RX_P0:
57                 return anx7808->rx_p0;
58         case RX_P1:
59                 return anx7808->rx_p1;
60         default:
61                 break;
62         }
63         DRM_ERROR("Invalid i2c address %04x.\n", addr);
64         return NULL;
65 }
66
67 static int anx7808_read_reg(struct anx7808_data *anx7808, uint16_t addr,
68                             uint8_t *value)
69 {
70         int ret = 0;
71         struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
72
73         if (client == NULL) {
74                 *value = 0;
75                 return -EINVAL;
76         }
77
78         ret = i2c_smbus_read_byte_data(client, addr & 0xFF);
79         if (ret < 0) {
80                 DRM_ERROR("Failed to read i2c addr=%04x.\n", addr);
81                 *value = 0;
82                 return ret;
83         }
84         *value = ret;
85         return 0;
86 }
87
88 static int anx7808_write_reg(struct anx7808_data *anx7808, uint16_t addr,
89                              uint8_t value)
90 {
91         int ret = 0;
92         struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
93
94         if (client == NULL)
95                 return -EINVAL;
96
97         ret = i2c_smbus_write_byte_data(client, addr & 0xFF, value);
98         if (ret < 0) {
99                 DRM_ERROR("Failed to write i2c addr=%04x.\n", addr);
100                 return -EIO;
101         }
102         return 0;
103 }
104
105 static int anx7808_set_bits(struct anx7808_data *anx7808, uint16_t addr,
106                             uint8_t bits)
107 {
108         uint8_t c;
109         int ret = 0;
110         ret = anx7808_read_reg(anx7808, addr, &c);
111         if (ret)
112                 return ret;
113         return anx7808_write_reg(anx7808, addr, c | bits);
114 }
115
116 static int anx7808_clear_bits(struct anx7808_data *anx7808, uint16_t addr,
117                               uint8_t bits)
118 {
119         uint8_t c;
120         int ret = 0;
121         ret = anx7808_read_reg(anx7808, addr, &c);
122         if (ret)
123                 return ret;
124         return anx7808_write_reg(anx7808, addr, c & (~bits));
125 }
126
127 static int anx7808_power_on(struct anx7808_data *anx7808)
128 {
129         int ret = 0;
130
131         DRM_INFO("Powering on ANX7808.\n");
132
133         gpio_set_value(anx7808->reset_gpio, 0);
134         usleep_range(1000, 2000);
135         gpio_set_value(anx7808->pd_gpio, 0);
136         usleep_range(2000, 4000);
137         ret = regulator_enable(anx7808->vdd_mydp);
138         if (ret < 0) {
139                 DRM_ERROR("Failed to power on ANX7808: %d", ret);
140                 return ret;
141         }
142         msleep(20);
143         gpio_set_value(anx7808->reset_gpio, 1);
144         return 0;
145 }
146
147 static int anx7808_power_off(struct anx7808_data *anx7808)
148 {
149         int ret = 0;
150
151         DRM_INFO("Powering off ANX7808.\n");
152
153         gpio_set_value(anx7808->reset_gpio, 0);
154         usleep_range(1000, 2000);
155         ret = regulator_disable(anx7808->vdd_mydp);
156         if (ret < 0) {
157                 DRM_ERROR("Failed to power off ANX7808: %d", ret);
158                 return ret;
159         }
160         usleep_range(5000, 10000);
161         gpio_set_value(anx7808->pd_gpio, 1);
162         usleep_range(1000, 2000);
163         return ret;
164 }
165
166 static int anx7808_chip_located(struct anx7808_data *anx7808)
167 {
168         int ret = 0;
169         uint16_t id;
170         uint8_t idh = 0, idl = 0;
171         ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDL_REG, &idl);
172         ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDH_REG, &idh);
173         if (ret)
174                 return ret;
175         id = idl | (idh << 8);
176         if (id != ANX7808_DEVICE_ID) {
177                 DRM_ERROR("ANX7808 not found.  ID reg contains: %04x\n", id);
178                 return -ENODEV;
179         }
180         DRM_INFO("ANX7808 found.\n");
181         return 0;
182 }
183
184 static int anx7808_aux_wait(struct anx7808_data *anx7808)
185 {
186         int err;
187         uint8_t status;
188         unsigned long start = jiffies;
189
190         while ((jiffies - start) <= msecs_to_jiffies(AUX_WAIT_MS)) {
191                 err = anx7808_read_reg(anx7808, SP_TX_AUX_STATUS, &status);
192                 if (err)
193                         return err;
194                 if (!(status & AUX_BUSY))
195                         break;
196                 usleep_range(100, 200);
197         }
198
199         if (status) {
200                 DRM_ERROR("Failed to read AUX channel: 0x%02x\n", status);
201                 return -EIO;
202         }
203         return 0;
204 }
205
206 static int anx7808_aux_read(struct anx7808_data *anx7808, uint32_t addr,
207                             uint8_t cmd, uint8_t count, uint8_t *pBuf)
208 {
209         int i;
210         int err = 0;
211         int addrl = (addr >> 0) & 0xFF;
212         int addrm = (addr >> 8) & 0xFF;
213         int addrh = (addr >> 16) & 0x0F;
214
215         if (count > AUX_BUFFER_SIZE)
216                 return -EINVAL;
217
218         err |= anx7808_write_reg(anx7808, SP_TX_BUF_DATA_COUNT_REG, 0x80);
219         err |= anx7808_write_reg(anx7808, SP_TX_AUX_CTRL_REG,
220                                  ((count - 1) << 4) | cmd);
221         err |= anx7808_write_reg(anx7808, SP_TX_AUX_ADDR_7_0_REG, addrl);
222         err |= anx7808_write_reg(anx7808, SP_TX_AUX_ADDR_15_8_REG, addrm);
223         err |= anx7808_write_reg(anx7808, SP_TX_AUX_ADDR_19_16_REG, addrh);
224         err |= anx7808_set_bits(anx7808, SP_TX_AUX_CTRL_REG2, AUX_OP_EN);
225         if (err)
226                 return -EIO;
227         usleep_range(2000, 4000);
228
229         err = anx7808_aux_wait(anx7808);
230         if (err)
231                 return err;
232
233         for (i = 0; i < count; i++)
234                 err |= anx7808_read_reg(anx7808, SP_TX_BUF_DATA_0_REG + i,
235                                         pBuf + i);
236         if (err)
237                 return -EIO;
238
239         return 0;
240 }
241
242 static int anx7808_aux_dpcd_read(struct anx7808_data *anx7808, uint32_t addr,
243                                  uint8_t count, uint8_t *pBuf)
244 {
245         return anx7808_aux_read(anx7808, addr, AUX_DPCD, count, pBuf);
246 }
247
248 static irqreturn_t anx7808_cable_det_isr(int irq, void *data)
249 {
250         struct anx7808_data *anx7808 = data;
251         uint8_t version;
252
253         DRM_INFO("Detected cable insertion.\n");
254
255         anx7808_power_on(anx7808);
256         anx7808_clear_bits(anx7808, SP_POWERD_CTRL_REG, REGISTER_PD);
257         anx7808_clear_bits(anx7808, SP_POWERD_CTRL_REG, TOTAL_PD);
258         anx7808_clear_bits(anx7808, SP_TX_VID_CTRL1_REG, VIDEO_MUTE);
259
260         anx7808_aux_dpcd_read(anx7808, US_COMM_2, 1, &version);
261         DRM_DEBUG_KMS("ANX7730 Firmware version 0x%02x.\n", (version & 0x7f));
262
263         return IRQ_HANDLED;
264 }
265
266 static void anx7808_free_gpios(struct anx7808_data *anx7808)
267 {
268         gpio_free(anx7808->cable_det_gpio);
269         gpio_free(anx7808->intp_gpio);
270         gpio_free(anx7808->reset_gpio);
271         gpio_free(anx7808->pd_gpio);
272 }
273
274 static void unregister_i2c_clients(struct anx7808_data *anx7808)
275 {
276         if (anx7808->rx_p1)
277                 i2c_unregister_device(anx7808->rx_p1);
278         if (anx7808->rx_p0)
279                 i2c_unregister_device(anx7808->rx_p0);
280         if (anx7808->tx_p2)
281                 i2c_unregister_device(anx7808->tx_p2);
282         if (anx7808->tx_p1)
283                 i2c_unregister_device(anx7808->tx_p1);
284 }
285
286 static int anx7808_probe(struct i2c_client *client,
287                         const struct i2c_device_id *id)
288 {
289         int ret = 0;
290         struct device_node *node = client->dev.of_node;
291         struct anx7808_data *anx7808;
292
293         anx7808 = devm_kzalloc(&client->dev, sizeof(struct anx7808_data),
294                                GFP_KERNEL);
295         if (!anx7808) {
296                 DRM_ERROR("Failed to allocate platform_data.\n");
297                 return -ENOMEM;
298         }
299         i2c_set_clientdata(client, anx7808);
300
301         anx7808->vdd_mydp = regulator_get(&client->dev, "vdd_mydp");
302         if (IS_ERR(anx7808->vdd_mydp)) {
303                 DRM_ERROR("Failed to find regulator vdd_mydp.\n");
304                 return PTR_ERR(anx7808->vdd_mydp);
305         }
306
307         anx7808->pd_gpio = of_get_named_gpio(node, "pd-gpio", 0);
308         if (!gpio_is_valid(anx7808->pd_gpio)) {
309                 DRM_ERROR("Failed to locate pd-gpio.\n");
310                 ret = anx7808->pd_gpio;
311                 goto err_reg;
312         }
313
314         anx7808->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
315         if (!gpio_is_valid(anx7808->reset_gpio)) {
316                 DRM_ERROR("Failed to locate reset-gpio.\n");
317                 ret = anx7808->reset_gpio;
318                 goto err_reg;
319         }
320
321         anx7808->intp_gpio = of_get_named_gpio(node, "intp-gpio", 0);
322         if (!gpio_is_valid(anx7808->intp_gpio)) {
323                 DRM_ERROR("Failed to locate intp-gpio.\n");
324                 ret = anx7808->intp_gpio;
325                 goto err_reg;
326         }
327
328         anx7808->cable_det_gpio = of_get_named_gpio(node, "cable-det-gpio", 0);
329         if (!gpio_is_valid(anx7808->cable_det_gpio)) {
330                 DRM_ERROR("Failed to locate cable-det-gpio.\n");
331                 ret = anx7808->cable_det_gpio;
332                 goto err_reg;
333         }
334
335         ret = gpio_request_one(anx7808->pd_gpio, GPIOF_OUT_INIT_HIGH,
336                                "anx7808_pd_gpio");
337         if (ret) {
338                 DRM_ERROR("Failed to request pd_gpio.\n");
339                 goto err_gpio;
340         }
341
342         ret = gpio_request_one(anx7808->reset_gpio, GPIOF_OUT_INIT_LOW,
343                                "anx7808_reset_gpio");
344         if (ret) {
345                 DRM_ERROR("Failed to request reset_gpio.\n");
346                 goto err_gpio;
347         }
348
349         ret = gpio_request_one(anx7808->intp_gpio, GPIOF_DIR_IN,
350                                "anx7808_intp_gpio");
351         if (ret) {
352                 DRM_ERROR("Failed to request intp_gpio.\n");
353                 goto err_gpio;
354         }
355
356         ret = gpio_request_one(anx7808->cable_det_gpio, GPIOF_DIR_IN,
357                                "anx7808_cable_det_gpio");
358         if (ret) {
359                 DRM_ERROR("Failed to request cable_det_gpio.\n");
360                 goto err_gpio;
361         }
362
363         anx7808->tx_p0 = client;
364         if (!((anx7808->tx_p0->addr) == TX_P0 >> 1)) {
365                 DRM_ERROR("I2C client address %02x != expected value %02x.\n",
366                           anx7808->tx_p0->addr, TX_P0 >> 1);
367                 goto err_i2c;
368         }
369         anx7808->tx_p1 = i2c_new_dummy(client->adapter, TX_P1 >> 1);
370         if (!anx7808->tx_p1) {
371                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P1 >> 1);
372                 ret = -EINVAL;
373                 goto err_i2c;
374         }
375         anx7808->tx_p2 = i2c_new_dummy(client->adapter, TX_P2 >> 1);
376         if (!anx7808->tx_p2) {
377                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P2 >> 1);
378                 ret = -EINVAL;
379                 goto err_i2c;
380         }
381         anx7808->rx_p0 = i2c_new_dummy(client->adapter, RX_P0 >> 1);
382         if (!anx7808->rx_p0) {
383                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P0 >> 1);
384                 ret = -EINVAL;
385                 goto err_i2c;
386         }
387         anx7808->rx_p1 = i2c_new_dummy(client->adapter, RX_P1 >> 1);
388         if (!anx7808->rx_p1) {
389                 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P1 >> 1);
390                 ret = -EINVAL;
391                 goto err_i2c;
392         }
393
394         anx7808_power_on(anx7808);
395         ret = anx7808_chip_located(anx7808);
396         anx7808_power_off(anx7808);
397         if (ret)
398                 goto err_i2c;
399
400         anx7808->cable_det_irq = gpio_to_irq(anx7808->cable_det_gpio);
401         if (anx7808->cable_det_irq < 0) {
402                 DRM_ERROR("Failed to get irq: %d\n", anx7808->cable_det_irq);
403                 goto err_i2c;
404         }
405
406         ret = devm_request_irq(&client->dev,
407                                anx7808->cable_det_irq,
408                                anx7808_cable_det_isr,
409                                IRQF_TRIGGER_RISING,
410                                "anx7808_cable_det",
411                                anx7808);
412         if (ret < 0) {
413                 DRM_ERROR("Failed to request irq: %d\n", ret);
414                 goto err_i2c;
415         }
416
417         DRM_INFO("ANX7808 initialization successful.\n");
418
419         return 0;
420 err_i2c:
421         unregister_i2c_clients(anx7808);
422 err_gpio:
423         anx7808_free_gpios(anx7808);
424 err_reg:
425         regulator_put(anx7808->vdd_mydp);
426         return ret;
427 }
428
429 static int anx7808_remove(struct i2c_client *client)
430 {
431         struct anx7808_data *anx7808 = i2c_get_clientdata(client);
432         unregister_i2c_clients(anx7808);
433         anx7808_free_gpios(anx7808);
434         regulator_put(anx7808->vdd_mydp);
435         return 0;
436 }
437
438 static const struct i2c_device_id anx7808_id[] = {
439         { "anx7808", 0 },
440         { }
441 };
442
443 MODULE_DEVICE_TABLE(i2c, anx7808_id);
444
445 static struct i2c_driver anx7808_driver = {
446         .driver = {
447                 .name = "anx7808",
448                 .owner = THIS_MODULE,
449         },
450         .probe = anx7808_probe,
451         .remove = anx7808_remove,
452         .id_table = anx7808_id,
453 };
454
455 static int __init anx7808_init(void)
456 {
457         int ret = 0;
458
459         ret = i2c_add_driver(&anx7808_driver);
460         if (ret < 0)
461                 DRM_ERROR("Failed to register anx7808 i2c driver.\n");
462         return ret;
463 }
464
465 static void __exit anx7808_exit(void)
466 {
467         i2c_del_driver(&anx7808_driver);
468 }
469
470 module_init(anx7808_init);
471 module_exit(anx7808_exit);
472
473 MODULE_DESCRIPTION("ANX7808 driver");
474 MODULE_AUTHOR("Jeremy Thorpe <jeremyt@chromium.org>");
475 MODULE_LICENSE("GPL");