1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  i2c_pca_platform.c
4  *
5  *  Platform driver for the PCA9564 I2C controller.
6  *
7  *  Copyright (C) 2008 Pengutronix
8  *
9 
10  */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/errno.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/i2c-algo-pca.h>
21 #include <linux/platform_data/i2c-pca-platform.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/io.h>
24 #include <linux/of.h>
25 
26 #include <asm/irq.h>
27 
28 struct i2c_pca_pf_data {
29 	void __iomem			*reg_base;
30 	int				irq;	/* if 0, use polling */
31 	struct gpio_desc		*gpio;
32 	wait_queue_head_t		wait;
33 	struct i2c_adapter		adap;
34 	struct i2c_algo_pca_data	algo_data;
35 };
36 
37 /* Read/Write functions for different register alignments */
38 
i2c_pca_pf_readbyte8(void * pd,int reg)39 static int i2c_pca_pf_readbyte8(void *pd, int reg)
40 {
41 	struct i2c_pca_pf_data *i2c = pd;
42 	return ioread8(i2c->reg_base + reg);
43 }
44 
i2c_pca_pf_readbyte16(void * pd,int reg)45 static int i2c_pca_pf_readbyte16(void *pd, int reg)
46 {
47 	struct i2c_pca_pf_data *i2c = pd;
48 	return ioread8(i2c->reg_base + reg * 2);
49 }
50 
i2c_pca_pf_readbyte32(void * pd,int reg)51 static int i2c_pca_pf_readbyte32(void *pd, int reg)
52 {
53 	struct i2c_pca_pf_data *i2c = pd;
54 	return ioread8(i2c->reg_base + reg * 4);
55 }
56 
i2c_pca_pf_writebyte8(void * pd,int reg,int val)57 static void i2c_pca_pf_writebyte8(void *pd, int reg, int val)
58 {
59 	struct i2c_pca_pf_data *i2c = pd;
60 	iowrite8(val, i2c->reg_base + reg);
61 }
62 
i2c_pca_pf_writebyte16(void * pd,int reg,int val)63 static void i2c_pca_pf_writebyte16(void *pd, int reg, int val)
64 {
65 	struct i2c_pca_pf_data *i2c = pd;
66 	iowrite8(val, i2c->reg_base + reg * 2);
67 }
68 
i2c_pca_pf_writebyte32(void * pd,int reg,int val)69 static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
70 {
71 	struct i2c_pca_pf_data *i2c = pd;
72 	iowrite8(val, i2c->reg_base + reg * 4);
73 }
74 
75 
i2c_pca_pf_waitforcompletion(void * pd)76 static int i2c_pca_pf_waitforcompletion(void *pd)
77 {
78 	struct i2c_pca_pf_data *i2c = pd;
79 	unsigned long timeout;
80 	long ret;
81 
82 	if (i2c->irq) {
83 		ret = wait_event_timeout(i2c->wait,
84 			i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
85 			& I2C_PCA_CON_SI, i2c->adap.timeout);
86 	} else {
87 		/* Do polling */
88 		timeout = jiffies + i2c->adap.timeout;
89 		do {
90 			ret = time_before(jiffies, timeout);
91 			if (i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
92 					& I2C_PCA_CON_SI)
93 				break;
94 			udelay(100);
95 		} while (ret);
96 	}
97 
98 	return ret > 0;
99 }
100 
i2c_pca_pf_dummyreset(void * pd)101 static void i2c_pca_pf_dummyreset(void *pd)
102 {
103 	struct i2c_pca_pf_data *i2c = pd;
104 
105 	dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n");
106 }
107 
i2c_pca_pf_resetchip(void * pd)108 static void i2c_pca_pf_resetchip(void *pd)
109 {
110 	struct i2c_pca_pf_data *i2c = pd;
111 
112 	gpiod_set_value(i2c->gpio, 1);
113 	ndelay(100);
114 	gpiod_set_value(i2c->gpio, 0);
115 }
116 
i2c_pca_pf_handler(int this_irq,void * dev_id)117 static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id)
118 {
119 	struct i2c_pca_pf_data *i2c = dev_id;
120 
121 	if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
122 		return IRQ_NONE;
123 
124 	wake_up(&i2c->wait);
125 
126 	return IRQ_HANDLED;
127 }
128 
129 
i2c_pca_pf_probe(struct platform_device * pdev)130 static int i2c_pca_pf_probe(struct platform_device *pdev)
131 {
132 	struct i2c_pca_pf_data *i2c;
133 	struct resource *res;
134 	struct i2c_pca9564_pf_platform_data *platform_data =
135 				dev_get_platdata(&pdev->dev);
136 	struct device_node *np = pdev->dev.of_node;
137 	int ret = 0;
138 	int irq;
139 
140 	irq = platform_get_irq_optional(pdev, 0);
141 	/* If irq is 0, we do polling. */
142 	if (irq < 0)
143 		irq = 0;
144 
145 	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
146 	if (!i2c)
147 		return -ENOMEM;
148 
149 	i2c->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
150 	if (IS_ERR(i2c->reg_base))
151 		return PTR_ERR(i2c->reg_base);
152 
153 
154 	init_waitqueue_head(&i2c->wait);
155 
156 	i2c->irq = irq;
157 
158 	i2c->adap.nr = pdev->id;
159 	i2c->adap.owner = THIS_MODULE;
160 	snprintf(i2c->adap.name, sizeof(i2c->adap.name),
161 		 "PCA9564/PCA9665 at 0x%08lx",
162 		 (unsigned long) res->start);
163 	i2c->adap.algo_data = &i2c->algo_data;
164 	i2c->adap.dev.parent = &pdev->dev;
165 	i2c->adap.dev.of_node = np;
166 
167 	i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
168 	if (IS_ERR(i2c->gpio))
169 		return PTR_ERR(i2c->gpio);
170 
171 	i2c->adap.timeout = HZ;
172 	ret = device_property_read_u32(&pdev->dev, "clock-frequency",
173 				       &i2c->algo_data.i2c_clock);
174 	if (ret)
175 		i2c->algo_data.i2c_clock = 59000;
176 
177 	if (platform_data) {
178 		i2c->adap.timeout = platform_data->timeout;
179 		i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
180 	}
181 
182 	i2c->algo_data.data = i2c;
183 	i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
184 	if (i2c->gpio)
185 		i2c->algo_data.reset_chip = i2c_pca_pf_resetchip;
186 	else
187 		i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset;
188 
189 	switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
190 	case IORESOURCE_MEM_32BIT:
191 		i2c->algo_data.write_byte = i2c_pca_pf_writebyte32;
192 		i2c->algo_data.read_byte = i2c_pca_pf_readbyte32;
193 		break;
194 	case IORESOURCE_MEM_16BIT:
195 		i2c->algo_data.write_byte = i2c_pca_pf_writebyte16;
196 		i2c->algo_data.read_byte = i2c_pca_pf_readbyte16;
197 		break;
198 	case IORESOURCE_MEM_8BIT:
199 	default:
200 		i2c->algo_data.write_byte = i2c_pca_pf_writebyte8;
201 		i2c->algo_data.read_byte = i2c_pca_pf_readbyte8;
202 		break;
203 	}
204 
205 	if (irq) {
206 		ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler,
207 			IRQF_TRIGGER_FALLING, pdev->name, i2c);
208 		if (ret)
209 			return ret;
210 	}
211 
212 	ret = i2c_pca_add_numbered_bus(&i2c->adap);
213 	if (ret)
214 		return ret;
215 
216 	platform_set_drvdata(pdev, i2c);
217 
218 	dev_info(&pdev->dev, "registered.\n");
219 
220 	return 0;
221 }
222 
i2c_pca_pf_remove(struct platform_device * pdev)223 static void i2c_pca_pf_remove(struct platform_device *pdev)
224 {
225 	struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
226 
227 	i2c_del_adapter(&i2c->adap);
228 }
229 
230 #ifdef CONFIG_OF
231 static const struct of_device_id i2c_pca_of_match_table[] = {
232 	{ .compatible = "nxp,pca9564" },
233 	{ .compatible = "nxp,pca9665" },
234 	{},
235 };
236 MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table);
237 #endif
238 
239 static struct platform_driver i2c_pca_pf_driver = {
240 	.probe = i2c_pca_pf_probe,
241 	.remove_new = i2c_pca_pf_remove,
242 	.driver = {
243 		.name = "i2c-pca-platform",
244 		.of_match_table = of_match_ptr(i2c_pca_of_match_table),
245 	},
246 };
247 
248 module_platform_driver(i2c_pca_pf_driver);
249 
250 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
251 MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
252 MODULE_LICENSE("GPL");
253