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
36 struct regulator *vdd_mydp;
37 struct i2c_client *tx_p0;
38 struct i2c_client *tx_p1;
39 struct i2c_client *tx_p2;
40 struct i2c_client *rx_p0;
41 struct i2c_client *rx_p1;
44 static struct i2c_client *anx7808_addr_to_client(struct anx7808_data *anx7808,
49 return anx7808->tx_p0;
51 return anx7808->tx_p1;
53 return anx7808->tx_p2;
55 return anx7808->rx_p0;
57 return anx7808->rx_p1;
61 DRM_ERROR("Invalid i2c address %04x.\n", addr);
65 static int anx7808_read_reg(struct anx7808_data *anx7808, uint16_t addr,
69 struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
76 ret = i2c_smbus_read_byte_data(client, addr & 0xff);
78 DRM_ERROR("Failed to read i2c addr=%04x.\n", addr);
86 static int anx7808_power_on(struct anx7808_data *anx7808)
90 DRM_INFO("Powering on ANX7808.\n");
92 gpio_set_value(anx7808->reset_gpio, 0);
93 usleep_range(1000, 2000);
94 gpio_set_value(anx7808->pd_gpio, 0);
95 usleep_range(2000, 4000);
96 ret = regulator_enable(anx7808->vdd_mydp);
98 DRM_ERROR("Failed to power on ANX7808: %d", ret);
102 gpio_set_value(anx7808->reset_gpio, 1);
106 static int anx7808_power_off(struct anx7808_data *anx7808)
110 DRM_INFO("Powering off ANX7808.\n");
112 gpio_set_value(anx7808->reset_gpio, 0);
113 usleep_range(1000, 2000);
114 ret = regulator_disable(anx7808->vdd_mydp);
116 DRM_ERROR("Failed to power off ANX7808: %d", ret);
119 usleep_range(5000, 10000);
120 gpio_set_value(anx7808->pd_gpio, 1);
121 usleep_range(1000, 2000);
125 static int anx7808_chip_located(struct anx7808_data *anx7808)
129 uint8_t idh = 0, idl = 0;
130 ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDL_REG, &idl);
131 ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDH_REG, &idh);
134 id = idl | (idh << 8);
135 if (id != ANX7808_DEVICE_ID) {
136 DRM_ERROR("ANX7808 not found. ID reg contains: %04x\n", id);
139 DRM_INFO("ANX7808 found.\n");
143 static irqreturn_t anx7808_cable_det_isr(int irq, void *data)
145 DRM_INFO("Detected cable insertion.\n");
150 static void anx7808_free_gpios(struct anx7808_data *anx7808)
152 gpio_free(anx7808->cable_det_gpio);
153 gpio_free(anx7808->intp_gpio);
154 gpio_free(anx7808->reset_gpio);
155 gpio_free(anx7808->pd_gpio);
158 static void unregister_i2c_clients(struct anx7808_data *anx7808)
161 i2c_unregister_device(anx7808->rx_p1);
163 i2c_unregister_device(anx7808->rx_p0);
165 i2c_unregister_device(anx7808->tx_p2);
167 i2c_unregister_device(anx7808->tx_p1);
170 static int anx7808_probe(struct i2c_client *client,
171 const struct i2c_device_id *id)
174 struct device_node *node = client->dev.of_node;
175 struct anx7808_data *anx7808;
177 anx7808 = devm_kzalloc(&client->dev, sizeof(struct anx7808_data),
180 DRM_ERROR("Failed to allocate platform_data.\n");
183 i2c_set_clientdata(client, anx7808);
185 anx7808->vdd_mydp = regulator_get(&client->dev, "vdd_mydp");
186 if (IS_ERR(anx7808->vdd_mydp)) {
187 DRM_ERROR("Failed to find regulator vdd_mydp.\n");
188 return PTR_ERR(anx7808->vdd_mydp);
191 anx7808->pd_gpio = of_get_named_gpio(node, "pd-gpio", 0);
192 if (!gpio_is_valid(anx7808->pd_gpio)) {
193 DRM_ERROR("Failed to locate pd-gpio.\n");
194 ret = anx7808->pd_gpio;
198 anx7808->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
199 if (!gpio_is_valid(anx7808->reset_gpio)) {
200 DRM_ERROR("Failed to locate reset-gpio.\n");
201 ret = anx7808->reset_gpio;
205 anx7808->intp_gpio = of_get_named_gpio(node, "intp-gpio", 0);
206 if (!gpio_is_valid(anx7808->intp_gpio)) {
207 DRM_ERROR("Failed to locate intp-gpio.\n");
208 ret = anx7808->intp_gpio;
212 anx7808->cable_det_gpio = of_get_named_gpio(node, "cable-det-gpio", 0);
213 if (!gpio_is_valid(anx7808->cable_det_gpio)) {
214 DRM_ERROR("Failed to locate cable-det-gpio.\n");
215 ret = anx7808->cable_det_gpio;
219 ret = gpio_request_one(anx7808->pd_gpio, GPIOF_OUT_INIT_HIGH,
222 DRM_ERROR("Failed to request pd_gpio.\n");
226 ret = gpio_request_one(anx7808->reset_gpio, GPIOF_OUT_INIT_LOW,
227 "anx7808_reset_gpio");
229 DRM_ERROR("Failed to request reset_gpio.\n");
233 ret = gpio_request_one(anx7808->intp_gpio, GPIOF_DIR_IN,
234 "anx7808_intp_gpio");
236 DRM_ERROR("Failed to request intp_gpio.\n");
240 ret = gpio_request_one(anx7808->cable_det_gpio, GPIOF_DIR_IN,
241 "anx7808_cable_det_gpio");
243 DRM_ERROR("Failed to request cable_det_gpio.\n");
247 anx7808->tx_p0 = client;
248 if (!((anx7808->tx_p0->addr) == TX_P0 >> 1)) {
249 DRM_ERROR("I2C client address %02x != expected value %02x.\n",
250 anx7808->tx_p0->addr, TX_P0 >> 1);
253 anx7808->tx_p1 = i2c_new_dummy(client->adapter, TX_P1 >> 1);
254 if (!anx7808->tx_p1) {
255 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P1 >> 1);
259 anx7808->tx_p2 = i2c_new_dummy(client->adapter, TX_P2 >> 1);
260 if (!anx7808->tx_p2) {
261 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P2 >> 1);
265 anx7808->rx_p0 = i2c_new_dummy(client->adapter, RX_P0 >> 1);
266 if (!anx7808->rx_p0) {
267 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P0 >> 1);
271 anx7808->rx_p1 = i2c_new_dummy(client->adapter, RX_P1 >> 1);
272 if (!anx7808->rx_p1) {
273 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P1 >> 1);
278 anx7808_power_on(anx7808);
279 ret = anx7808_chip_located(anx7808);
280 anx7808_power_off(anx7808);
284 anx7808->cable_det_irq = gpio_to_irq(anx7808->cable_det_gpio);
285 if (anx7808->cable_det_irq < 0) {
286 DRM_ERROR("Failed to get irq: %d\n", anx7808->cable_det_irq);
290 ret = devm_request_irq(&client->dev,
291 anx7808->cable_det_irq,
292 anx7808_cable_det_isr,
297 DRM_ERROR("Failed to request irq: %d\n", ret);
301 DRM_INFO("ANX7808 initialization successful.\n");
305 unregister_i2c_clients(anx7808);
307 anx7808_free_gpios(anx7808);
309 regulator_put(anx7808->vdd_mydp);
313 static int anx7808_remove(struct i2c_client *client)
315 struct anx7808_data *anx7808 = i2c_get_clientdata(client);
316 unregister_i2c_clients(anx7808);
317 anx7808_free_gpios(anx7808);
318 regulator_put(anx7808->vdd_mydp);
322 static const struct i2c_device_id anx7808_id[] = {
327 MODULE_DEVICE_TABLE(i2c, anx7808_id);
329 static struct i2c_driver anx7808_driver = {
332 .owner = THIS_MODULE,
334 .probe = anx7808_probe,
335 .remove = anx7808_remove,
336 .id_table = anx7808_id,
339 static int __init anx7808_init(void)
343 ret = i2c_add_driver(&anx7808_driver);
345 DRM_ERROR("Failed to register anx7808 i2c driver.\n");
349 static void __exit anx7808_exit(void)
351 i2c_del_driver(&anx7808_driver);
354 module_init(anx7808_init);
355 module_exit(anx7808_exit);
357 MODULE_DESCRIPTION("ANX7808 driver");
358 MODULE_AUTHOR("Jeremy Thorpe <jeremyt@chromium.org>");
359 MODULE_LICENSE("GPL");