2 * Copyright (C) 2013 Google, Inc.
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.
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.
13 * Based on code obtained from Analogix Inc with copyright:
14 * Copyright(c) 2012, Analogix Semiconductor. All rights reserved.
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>
26 #include "anx7808regs.h"
28 #define ANX7808_DEVICE_ID 0x7808
29 #define AUX_WAIT_MS 100
30 #define AUX_BUFFER_SIZE 0x10
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;
46 static struct i2c_client *anx7808_addr_to_client(struct anx7808_data *anx7808,
51 return anx7808->tx_p0;
53 return anx7808->tx_p1;
55 return anx7808->tx_p2;
57 return anx7808->rx_p0;
59 return anx7808->rx_p1;
63 DRM_ERROR("Invalid i2c address %04x.\n", addr);
67 static int anx7808_read_reg(struct anx7808_data *anx7808, uint16_t addr,
71 struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
78 ret = i2c_smbus_read_byte_data(client, addr & 0xFF);
80 DRM_ERROR("Failed to read i2c addr=%04x.\n", addr);
88 static int anx7808_write_reg(struct anx7808_data *anx7808, uint16_t addr,
92 struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
97 ret = i2c_smbus_write_byte_data(client, addr & 0xFF, value);
99 DRM_ERROR("Failed to write i2c addr=%04x.\n", addr);
105 static int anx7808_set_bits(struct anx7808_data *anx7808, uint16_t addr,
110 ret = anx7808_read_reg(anx7808, addr, &c);
113 return anx7808_write_reg(anx7808, addr, c | bits);
116 static int anx7808_clear_bits(struct anx7808_data *anx7808, uint16_t addr,
121 ret = anx7808_read_reg(anx7808, addr, &c);
124 return anx7808_write_reg(anx7808, addr, c & (~bits));
127 static int anx7808_power_on(struct anx7808_data *anx7808)
131 DRM_INFO("Powering on ANX7808.\n");
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);
139 DRM_ERROR("Failed to power on ANX7808: %d", ret);
143 gpio_set_value(anx7808->reset_gpio, 1);
147 static int anx7808_power_off(struct anx7808_data *anx7808)
151 DRM_INFO("Powering off ANX7808.\n");
153 gpio_set_value(anx7808->reset_gpio, 0);
154 usleep_range(1000, 2000);
155 ret = regulator_disable(anx7808->vdd_mydp);
157 DRM_ERROR("Failed to power off ANX7808: %d", ret);
160 usleep_range(5000, 10000);
161 gpio_set_value(anx7808->pd_gpio, 1);
162 usleep_range(1000, 2000);
166 static int anx7808_chip_located(struct anx7808_data *anx7808)
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);
175 id = idl | (idh << 8);
176 if (id != ANX7808_DEVICE_ID) {
177 DRM_ERROR("ANX7808 not found. ID reg contains: %04x\n", id);
180 DRM_INFO("ANX7808 found.\n");
184 static int anx7808_aux_wait(struct anx7808_data *anx7808)
188 unsigned long start = jiffies;
190 while ((jiffies - start) <= msecs_to_jiffies(AUX_WAIT_MS)) {
191 err = anx7808_read_reg(anx7808, SP_TX_AUX_STATUS, &status);
194 if (!(status & AUX_BUSY))
196 usleep_range(100, 200);
200 DRM_ERROR("Failed to read AUX channel: 0x%02x\n", status);
206 static int anx7808_aux_read(struct anx7808_data *anx7808, uint32_t addr,
207 uint8_t cmd, uint8_t count, uint8_t *pBuf)
211 int addrl = (addr >> 0) & 0xFF;
212 int addrm = (addr >> 8) & 0xFF;
213 int addrh = (addr >> 16) & 0x0F;
215 if (count > AUX_BUFFER_SIZE)
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);
227 usleep_range(2000, 4000);
229 err = anx7808_aux_wait(anx7808);
233 for (i = 0; i < count; i++)
234 err |= anx7808_read_reg(anx7808, SP_TX_BUF_DATA_0_REG + i,
242 static int anx7808_aux_dpcd_read(struct anx7808_data *anx7808, uint32_t addr,
243 uint8_t count, uint8_t *pBuf)
245 return anx7808_aux_read(anx7808, addr, AUX_DPCD, count, pBuf);
248 static irqreturn_t anx7808_cable_det_isr(int irq, void *data)
250 struct anx7808_data *anx7808 = data;
253 DRM_INFO("Detected cable insertion.\n");
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);
260 anx7808_aux_dpcd_read(anx7808, US_COMM_2, 1, &version);
261 DRM_DEBUG_KMS("ANX7730 Firmware version 0x%02x.\n", (version & 0x7f));
266 static void anx7808_free_gpios(struct anx7808_data *anx7808)
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);
274 static void unregister_i2c_clients(struct anx7808_data *anx7808)
277 i2c_unregister_device(anx7808->rx_p1);
279 i2c_unregister_device(anx7808->rx_p0);
281 i2c_unregister_device(anx7808->tx_p2);
283 i2c_unregister_device(anx7808->tx_p1);
286 static int anx7808_probe(struct i2c_client *client,
287 const struct i2c_device_id *id)
290 struct device_node *node = client->dev.of_node;
291 struct anx7808_data *anx7808;
293 anx7808 = devm_kzalloc(&client->dev, sizeof(struct anx7808_data),
296 DRM_ERROR("Failed to allocate platform_data.\n");
299 i2c_set_clientdata(client, anx7808);
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);
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;
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;
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;
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;
335 ret = gpio_request_one(anx7808->pd_gpio, GPIOF_OUT_INIT_HIGH,
338 DRM_ERROR("Failed to request pd_gpio.\n");
342 ret = gpio_request_one(anx7808->reset_gpio, GPIOF_OUT_INIT_LOW,
343 "anx7808_reset_gpio");
345 DRM_ERROR("Failed to request reset_gpio.\n");
349 ret = gpio_request_one(anx7808->intp_gpio, GPIOF_DIR_IN,
350 "anx7808_intp_gpio");
352 DRM_ERROR("Failed to request intp_gpio.\n");
356 ret = gpio_request_one(anx7808->cable_det_gpio, GPIOF_DIR_IN,
357 "anx7808_cable_det_gpio");
359 DRM_ERROR("Failed to request cable_det_gpio.\n");
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);
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);
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);
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);
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);
394 anx7808_power_on(anx7808);
395 ret = anx7808_chip_located(anx7808);
396 anx7808_power_off(anx7808);
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);
406 ret = devm_request_irq(&client->dev,
407 anx7808->cable_det_irq,
408 anx7808_cable_det_isr,
413 DRM_ERROR("Failed to request irq: %d\n", ret);
417 DRM_INFO("ANX7808 initialization successful.\n");
421 unregister_i2c_clients(anx7808);
423 anx7808_free_gpios(anx7808);
425 regulator_put(anx7808->vdd_mydp);
429 static int anx7808_remove(struct i2c_client *client)
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);
438 static const struct i2c_device_id anx7808_id[] = {
443 MODULE_DEVICE_TABLE(i2c, anx7808_id);
445 static struct i2c_driver anx7808_driver = {
448 .owner = THIS_MODULE,
450 .probe = anx7808_probe,
451 .remove = anx7808_remove,
452 .id_table = anx7808_id,
455 static int __init anx7808_init(void)
459 ret = i2c_add_driver(&anx7808_driver);
461 DRM_ERROR("Failed to register anx7808 i2c driver.\n");
465 static void __exit anx7808_exit(void)
467 i2c_del_driver(&anx7808_driver);
470 module_init(anx7808_init);
471 module_exit(anx7808_exit);
473 MODULE_DESCRIPTION("ANX7808 driver");
474 MODULE_AUTHOR("Jeremy Thorpe <jeremyt@chromium.org>");
475 MODULE_LICENSE("GPL");