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
35 struct regulator *vdd_mydp;
36 struct i2c_client *tx_p0;
37 struct i2c_client *tx_p1;
38 struct i2c_client *tx_p2;
39 struct i2c_client *rx_p0;
40 struct i2c_client *rx_p1;
43 static struct i2c_client *anx7808_addr_to_client(struct anx7808_data *anx7808,
48 return anx7808->tx_p0;
50 return anx7808->tx_p1;
52 return anx7808->tx_p2;
54 return anx7808->rx_p0;
56 return anx7808->rx_p1;
60 DRM_ERROR("Invalid i2c address %04x.\n", addr);
64 static int anx7808_read_reg(struct anx7808_data *anx7808, uint16_t addr,
68 struct i2c_client *client = anx7808_addr_to_client(anx7808, addr);
75 ret = i2c_smbus_read_byte_data(client, addr & 0xff);
77 DRM_ERROR("Failed to read i2c addr=%04x.\n", addr);
85 static int anx7808_power_on(struct anx7808_data *anx7808)
89 DRM_INFO("Powering on ANX7808.\n");
91 gpio_set_value(anx7808->reset_gpio, 0);
92 usleep_range(1000, 2000);
93 gpio_set_value(anx7808->pd_gpio, 0);
94 usleep_range(2000, 4000);
95 ret = regulator_enable(anx7808->vdd_mydp);
97 DRM_ERROR("Failed to power on ANX7808: %d", ret);
101 gpio_set_value(anx7808->reset_gpio, 1);
105 static int anx7808_power_off(struct anx7808_data *anx7808)
109 DRM_INFO("Powering off ANX7808.\n");
111 gpio_set_value(anx7808->reset_gpio, 0);
112 usleep_range(1000, 2000);
113 ret = regulator_disable(anx7808->vdd_mydp);
115 DRM_ERROR("Failed to power off ANX7808: %d", ret);
118 usleep_range(5000, 10000);
119 gpio_set_value(anx7808->pd_gpio, 1);
120 usleep_range(1000, 2000);
124 static int anx7808_chip_located(struct anx7808_data *anx7808)
128 uint8_t idh = 0, idl = 0;
129 ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDL_REG, &idl);
130 ret |= anx7808_read_reg(anx7808, SP_TX_DEV_IDH_REG, &idh);
133 id = idl | (idh << 8);
134 if (id != ANX7808_DEVICE_ID) {
135 DRM_ERROR("ANX7808 not found. ID reg contains: %04x\n", id);
138 DRM_INFO("ANX7808 found.\n");
142 static void anx7808_free_gpios(struct anx7808_data *anx7808)
144 gpio_free(anx7808->cable_det_gpio);
145 gpio_free(anx7808->intp_gpio);
146 gpio_free(anx7808->reset_gpio);
147 gpio_free(anx7808->pd_gpio);
150 static void unregister_i2c_clients(struct anx7808_data *anx7808)
153 i2c_unregister_device(anx7808->rx_p1);
155 i2c_unregister_device(anx7808->rx_p0);
157 i2c_unregister_device(anx7808->tx_p2);
159 i2c_unregister_device(anx7808->tx_p1);
162 static int anx7808_probe(struct i2c_client *client,
163 const struct i2c_device_id *id)
166 struct device_node *node = client->dev.of_node;
167 struct anx7808_data *anx7808;
169 anx7808 = devm_kzalloc(&client->dev, sizeof(struct anx7808_data),
172 DRM_ERROR("Failed to allocate platform_data.\n");
175 i2c_set_clientdata(client, anx7808);
177 anx7808->vdd_mydp = regulator_get(&client->dev, "vdd_mydp");
178 if (IS_ERR(anx7808->vdd_mydp)) {
179 DRM_ERROR("Failed to find regulator vdd_mydp.\n");
180 return PTR_ERR(anx7808->vdd_mydp);
183 anx7808->pd_gpio = of_get_named_gpio(node, "pd-gpio", 0);
184 if (!gpio_is_valid(anx7808->pd_gpio)) {
185 DRM_ERROR("Failed to locate pd-gpio.\n");
186 ret = anx7808->pd_gpio;
190 anx7808->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
191 if (!gpio_is_valid(anx7808->reset_gpio)) {
192 DRM_ERROR("Failed to locate reset-gpio.\n");
193 ret = anx7808->reset_gpio;
197 anx7808->intp_gpio = of_get_named_gpio(node, "intp-gpio", 0);
198 if (!gpio_is_valid(anx7808->intp_gpio)) {
199 DRM_ERROR("Failed to locate intp-gpio.\n");
200 ret = anx7808->intp_gpio;
204 anx7808->cable_det_gpio = of_get_named_gpio(node, "cable-det-gpio", 0);
205 if (!gpio_is_valid(anx7808->cable_det_gpio)) {
206 DRM_ERROR("Failed to locate cable-det-gpio.\n");
207 ret = anx7808->cable_det_gpio;
211 ret = gpio_request_one(anx7808->pd_gpio, GPIOF_OUT_INIT_HIGH,
214 DRM_ERROR("Failed to request pd_gpio.\n");
218 ret = gpio_request_one(anx7808->reset_gpio, GPIOF_OUT_INIT_LOW,
219 "anx7808_reset_gpio");
221 DRM_ERROR("Failed to request reset_gpio.\n");
225 ret = gpio_request_one(anx7808->intp_gpio, GPIOF_DIR_IN,
226 "anx7808_intp_gpio");
228 DRM_ERROR("Failed to request intp_gpio.\n");
232 ret = gpio_request_one(anx7808->cable_det_gpio, GPIOF_DIR_IN,
233 "anx7808_cable_det_gpio");
235 DRM_ERROR("Failed to request cable_det_gpio.\n");
239 anx7808->tx_p0 = client;
240 if (!((anx7808->tx_p0->addr) == TX_P0 >> 1)) {
241 DRM_ERROR("I2C client address %02x != expected value %02x.\n",
242 anx7808->tx_p0->addr, TX_P0 >> 1);
245 anx7808->tx_p1 = i2c_new_dummy(client->adapter, TX_P1 >> 1);
246 if (!anx7808->tx_p1) {
247 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P1 >> 1);
251 anx7808->tx_p2 = i2c_new_dummy(client->adapter, TX_P2 >> 1);
252 if (!anx7808->tx_p2) {
253 DRM_ERROR("Failed to reserve i2c bus %02x.\n", TX_P2 >> 1);
257 anx7808->rx_p0 = i2c_new_dummy(client->adapter, RX_P0 >> 1);
258 if (!anx7808->rx_p0) {
259 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P0 >> 1);
263 anx7808->rx_p1 = i2c_new_dummy(client->adapter, RX_P1 >> 1);
264 if (!anx7808->rx_p1) {
265 DRM_ERROR("Failed to reserve i2c bus %02x.\n", RX_P1 >> 1);
270 anx7808_power_on(anx7808);
271 ret = anx7808_chip_located(anx7808);
272 anx7808_power_off(anx7808);
276 DRM_INFO("ANX7808 initialization successful.\n");
280 unregister_i2c_clients(anx7808);
282 anx7808_free_gpios(anx7808);
284 regulator_put(anx7808->vdd_mydp);
288 static int anx7808_remove(struct i2c_client *client)
290 struct anx7808_data *anx7808 = i2c_get_clientdata(client);
291 unregister_i2c_clients(anx7808);
292 anx7808_free_gpios(anx7808);
293 regulator_put(anx7808->vdd_mydp);
297 static const struct i2c_device_id anx7808_id[] = {
302 MODULE_DEVICE_TABLE(i2c, anx7808_id);
304 static struct i2c_driver anx7808_driver = {
307 .owner = THIS_MODULE,
309 .probe = anx7808_probe,
310 .remove = anx7808_remove,
311 .id_table = anx7808_id,
314 static int __init anx7808_init(void)
318 ret = i2c_add_driver(&anx7808_driver);
320 DRM_ERROR("Failed to register anx7808 i2c driver.\n");
324 static void __exit anx7808_exit(void)
326 i2c_del_driver(&anx7808_driver);
329 module_init(anx7808_init);
330 module_exit(anx7808_exit);
332 MODULE_DESCRIPTION("ANX7808 driver");
333 MODULE_AUTHOR("Jeremy Thorpe <jeremyt@chromium.org>");
334 MODULE_LICENSE("GPL");