xref: /OK3568_Linux_fs/kernel/drivers/gpio/gpio-aw9110.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for aw9110 I2C GPIO expanders
4  *
5  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
6  */
7 #include <linux/gpio/driver.h>
8 #include <linux/i2c.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/irqdomain.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/of_gpio.h>
19 
20 #define REG_INPUT_P0        0x00
21 #define REG_INPUT_P1        0x01
22 #define REG_OUTPUT_P0       0x02
23 #define REG_OUTPUT_P1       0x03
24 #define REG_CONFIG_P0       0x04
25 #define REG_CONFIG_P1       0x05
26 #define REG_INT_P0          0x06
27 #define REG_INT_P1          0x07
28 #define REG_ID              0x10
29 #define REG_CTRL            0x11
30 #define REG_WORK_MODE_P0    0x12
31 #define REG_WORK_MODE_P1    0x13
32 #define REG_EN_BREATH       0x14
33 #define REG_FADE_TIME       0x15
34 #define REG_FULL_TIME       0x16
35 #define REG_DLY0_BREATH     0x17
36 #define REG_DLY1_BREATH     0x18
37 #define REG_DLY2_BREATH     0x19
38 #define REG_DLY3_BREATH     0x1a
39 #define REG_DLY4_BREATH     0x1b
40 #define REG_DLY5_BREATH     0x1c
41 #define REG_DIM00           0x20
42 #define REG_DIM01           0x21
43 #define REG_DIM02           0x22
44 #define REG_DIM03           0x23
45 #define REG_DIM04           0x24
46 #define REG_DIM05           0x25
47 #define REG_DIM06           0x26
48 #define REG_DIM07           0x27
49 #define REG_DIM08           0x28
50 #define REG_DIM09           0x29
51 #define REG_SWRST           0x7F
52 #define REG_81H             0x81
53 
54 
55 static const struct i2c_device_id aw9110_id[] = {
56 	{ "aw9110", 10 },
57 	{ }
58 };
59 MODULE_DEVICE_TABLE(i2c, aw9110_id);
60 
61 #ifdef CONFIG_OF
62 static const struct of_device_id aw9110_of_table[] = {
63 	{ .compatible = "awinic,aw9110" },
64 	{ }
65 };
66 MODULE_DEVICE_TABLE(of, aw9110_of_table);
67 #endif
68 
69 
70 struct aw9110 {
71 	struct gpio_chip	chip;
72 	struct irq_chip		irqchip;
73 	struct i2c_client	*client;
74 	struct mutex		lock;		/* protect 'out' */
75 	unsigned int		out;		/* software latch */
76 	unsigned int		direct;		/* gpio direct */
77 	unsigned int		status;		/* current status */
78 	unsigned int		irq_enabled;	/* enabled irqs */
79 
80 	struct device		*dev;
81 	int			shdn_en;	/* shutdown ctrl */
82 
83 	int (*write)(struct i2c_client *client, u8 reg, u8 data);
84 	int (*read)(struct i2c_client *client, u8 reg);
85 };
86 
87 
aw9110_i2c_write_le8(struct i2c_client * client,u8 reg,u8 data)88 static int aw9110_i2c_write_le8(struct i2c_client *client, u8 reg, u8 data)
89 {
90 	return i2c_smbus_write_byte_data(client, reg, data);
91 }
92 
aw9110_i2c_read_le8(struct i2c_client * client,u8 reg)93 static int aw9110_i2c_read_le8(struct i2c_client *client, u8 reg)
94 {
95 	return (int)i2c_smbus_read_byte_data(client, reg);
96 }
97 
aw9110_get(struct gpio_chip * chip,unsigned int offset)98 static int aw9110_get(struct gpio_chip *chip, unsigned int offset)
99 {
100 	struct aw9110	*gpio = gpiochip_get_data(chip);
101 	int value = 0;
102 
103 	mutex_lock(&gpio->lock);
104 
105 	if (offset < 4) {
106 		value = gpio->read(gpio->client, REG_INPUT_P1);
107 		mutex_unlock(&gpio->lock);
108 
109 		value = (value < 0) ? value : !!(value & (1 << offset));
110 	} else {
111 		value = gpio->read(gpio->client, REG_INPUT_P0);
112 		mutex_unlock(&gpio->lock);
113 
114 		value = (value < 0) ? value : !!((value<<4) & (1 << offset));
115 	}
116 
117 	return value;
118 }
119 
aw9110_get_direction(struct gpio_chip * chip,unsigned int offset)120 static int aw9110_get_direction(struct gpio_chip *chip, unsigned int offset)
121 {
122 	struct aw9110	*gpio = gpiochip_get_data(chip);
123 	unsigned int reg_val;
124 
125 	reg_val = gpio->direct;
126 
127 	dev_dbg(gpio->dev, "direct get: %04X, pin:%d\n", reg_val, offset);
128 
129 	if (reg_val & (1<<offset))
130 		return GPIO_LINE_DIRECTION_IN;
131 
132 	return GPIO_LINE_DIRECTION_OUT;
133 }
134 
aw9110_direction_input(struct gpio_chip * chip,unsigned int offset)135 static int aw9110_direction_input(struct gpio_chip *chip, unsigned int offset)
136 {
137 	struct aw9110	*gpio = gpiochip_get_data(chip);
138 
139 	mutex_lock(&gpio->lock);
140 
141 	/* set direct */
142 	gpio->direct |= (1<<offset);
143 
144 	if (offset < 4)
145 		gpio->write(gpio->client, REG_CONFIG_P1, gpio->direct&0x0F);
146 	else
147 		gpio->write(gpio->client, REG_CONFIG_P0, (gpio->direct >> 4)&0x3F);
148 
149 	mutex_unlock(&gpio->lock);
150 
151 	dev_dbg(gpio->dev, "direct in: %04X, pin:%d\n", gpio->direct, offset);
152 
153 	return 0;
154 }
155 
aw9110_direction_output(struct gpio_chip * chip,unsigned int offset,int value)156 static int aw9110_direction_output(struct gpio_chip *chip, unsigned int offset, int value)
157 {
158 	struct aw9110	*gpio = gpiochip_get_data(chip);
159 
160 	/* set level */
161 	chip->set(chip, offset, value);
162 
163 	mutex_lock(&gpio->lock);
164 
165 	/* set direct */
166 	gpio->direct &= ~(1<<offset);
167 
168 	if (offset < 4)
169 		gpio->write(gpio->client, REG_CONFIG_P1, gpio->direct&0x0F);
170 	else
171 		gpio->write(gpio->client, REG_CONFIG_P0, (gpio->direct >> 4)&0x3F);
172 
173 	mutex_unlock(&gpio->lock);
174 
175 	dev_dbg(gpio->dev, "direct out: %04X, pin:%d\n", gpio->direct, offset);
176 	return 0;
177 }
178 
aw9110_set(struct gpio_chip * chip,unsigned int offset,int value)179 static void aw9110_set(struct gpio_chip *chip, unsigned int offset, int value)
180 {
181 	struct aw9110 *gpio = gpiochip_get_data(chip);
182 	unsigned int bit = 1 << offset;
183 
184 	mutex_lock(&gpio->lock);
185 
186 	if (value)
187 		gpio->out |= bit;
188 	else
189 		gpio->out &= ~bit;
190 
191 	if (offset < 4)
192 		gpio->write(gpio->client, REG_OUTPUT_P1, gpio->out >> 0);
193 	else
194 		gpio->write(gpio->client, REG_OUTPUT_P0, gpio->out >> 4);
195 
196 	mutex_unlock(&gpio->lock);
197 }
198 
199 /*-------------------------------------------------------------------------*/
200 
aw9110_irq(int irq,void * data)201 static irqreturn_t aw9110_irq(int irq, void *data)
202 {
203 	struct aw9110  *gpio = data;
204 	unsigned long change, i, status = 0;
205 
206 	int value = 0;
207 	int nirq;
208 
209 	value = gpio->read(gpio->client, REG_INPUT_P1);
210 	status |= (value < 0) ? 0 : value;
211 
212 	value = gpio->read(gpio->client, REG_INPUT_P0);
213 	status |= (value < 0) ? 0 : (value<<4);
214 
215 
216 	/*
217 	 * call the interrupt handler iff gpio is used as
218 	 * interrupt source, just to avoid bad irqs
219 	 */
220 	mutex_lock(&gpio->lock);
221 	change = (gpio->status ^ status) & gpio->irq_enabled;
222 	gpio->status = status;
223 	mutex_unlock(&gpio->lock);
224 
225 	for_each_set_bit(i, &change, gpio->chip.ngpio) {
226 		nirq = irq_find_mapping(gpio->chip.irq.domain, i);
227 		if (nirq) {
228 			dev_dbg(gpio->dev, "status:%04lx,change:%04lx,index:%ld,nirq:%d\n",
229 					status, change, i, nirq);
230 			handle_nested_irq(nirq);
231 		}
232 	}
233 
234 	return IRQ_HANDLED;
235 }
236 
237 /*
238  * NOP functions
239  */
aw9110_noop(struct irq_data * data)240 static void aw9110_noop(struct irq_data *data) { }
241 
aw9110_irq_set_wake(struct irq_data * data,unsigned int on)242 static int aw9110_irq_set_wake(struct irq_data *data, unsigned int on)
243 {
244 	struct aw9110 *gpio = irq_data_get_irq_chip_data(data);
245 
246 	return irq_set_irq_wake(gpio->client->irq, on);
247 }
248 
aw9110_irq_enable(struct irq_data * data)249 static void aw9110_irq_enable(struct irq_data *data)
250 {
251 	struct aw9110 *gpio = irq_data_get_irq_chip_data(data);
252 
253 	gpio->irq_enabled |= (1 << data->hwirq);
254 }
255 
aw9110_irq_disable(struct irq_data * data)256 static void aw9110_irq_disable(struct irq_data *data)
257 {
258 	struct aw9110 *gpio = irq_data_get_irq_chip_data(data);
259 
260 	gpio->irq_enabled &= ~(1 << data->hwirq);
261 }
262 
aw9110_irq_bus_lock(struct irq_data * data)263 static void aw9110_irq_bus_lock(struct irq_data *data)
264 {
265 	struct aw9110 *gpio = irq_data_get_irq_chip_data(data);
266 
267 	mutex_lock(&gpio->lock);
268 }
269 
aw9110_irq_bus_sync_unlock(struct irq_data * data)270 static void aw9110_irq_bus_sync_unlock(struct irq_data *data)
271 {
272 	struct aw9110 *gpio = irq_data_get_irq_chip_data(data);
273 
274 	mutex_unlock(&gpio->lock);
275 }
276 
aw9110_state_init(struct aw9110 * gpio)277 static void aw9110_state_init(struct aw9110	*gpio)
278 {
279 	/* out4-9 push-pull */
280 	gpio->write(gpio->client, REG_CTRL, (1<<4));
281 
282 	/* work mode : gpio */
283 	gpio->write(gpio->client, REG_WORK_MODE_P1, 0x0F);
284 	gpio->write(gpio->client, REG_WORK_MODE_P0, 0x3F);
285 
286 	/* default direct */
287 	gpio->direct = 0x03FF;	/* 0: output, 1:input */
288 	gpio->write(gpio->client, REG_CONFIG_P1, gpio->direct & 0x0F);
289 	gpio->write(gpio->client, REG_CONFIG_P0, (gpio->direct>>4) & 0x3F);
290 
291 	/* interrupt enable */
292 	gpio->irq_enabled = 0x03FF;	/* 0: disable 1:enable, chip: 0:enable,  1: disable */
293 	gpio->write(gpio->client, REG_INT_P1, ((~gpio->irq_enabled) >> 0)&0x0F);
294 	gpio->write(gpio->client, REG_INT_P0, ((~gpio->irq_enabled) >> 4)&0x3F);
295 
296 	/* clear interrupt */
297 	gpio->read(gpio->client, REG_INPUT_P1);
298 	gpio->read(gpio->client, REG_INPUT_P1);
299 }
300 
aw9110_parse_dt(struct aw9110 * chip,struct i2c_client * client)301 static int aw9110_parse_dt(struct aw9110 *chip, struct i2c_client *client)
302 {
303 	struct device_node *np = client->dev.of_node;
304 	int ret = 0;
305 
306 	/* shdn_en */
307 	ret = of_get_named_gpio(np, "shdn_en", 0);
308 	if (ret < 0) {
309 		dev_err(chip->dev, "of get shdn_en failed\n");
310 		chip->shdn_en = -1;
311 	} else {
312 		chip->shdn_en = ret;
313 
314 		ret = devm_gpio_request_one(chip->dev, chip->shdn_en,
315 					    GPIOF_OUT_INIT_LOW, "AW9110_SHDN_EN");
316 		if (ret) {
317 			dev_err(chip->dev,
318 				"devm_gpio_request_one shdn_en failed\n");
319 			return ret;
320 		}
321 
322 		/* enable chip */
323 		gpio_set_value(chip->shdn_en, 1);
324 	}
325 
326 	return 0;
327 }
328 
aw9110_check_dev_id(struct i2c_client * client)329 static int aw9110_check_dev_id(struct i2c_client *client)
330 {
331 	int ret;
332 
333 	ret = aw9110_i2c_read_le8(client, REG_ID);
334 
335 	if (ret < 0) {
336 		dev_err(&client->dev, "fail to read dev id(%d)\n", ret);
337 		return ret;
338 	}
339 
340 	dev_info(&client->dev, "dev id : 0x%02x\n", ret);
341 
342 	return 0;
343 }
344 
345 /*-------------------------------------------------------------------------*/
346 
aw9110_probe(struct i2c_client * client,const struct i2c_device_id * id)347 static int aw9110_probe(struct i2c_client *client,
348 			 const struct i2c_device_id *id)
349 {
350 	struct aw9110			*gpio;
351 	int				status;
352 
353 	dev_info(&client->dev, "===aw9110 probe===\n");
354 
355 	/* Allocate, initialize, and register this gpio_chip. */
356 	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
357 	if (!gpio)
358 		return -ENOMEM;
359 
360 	gpio->dev = &client->dev;
361 
362 	aw9110_parse_dt(gpio, client);
363 
364 	mutex_init(&gpio->lock);
365 
366 	gpio->chip.base			= -1;
367 	gpio->chip.can_sleep		= true;
368 	gpio->chip.parent		= &client->dev;
369 	gpio->chip.owner		= THIS_MODULE;
370 	gpio->chip.get			= aw9110_get;
371 	gpio->chip.set			= aw9110_set;
372 	gpio->chip.get_direction	= aw9110_get_direction;
373 	gpio->chip.direction_input	= aw9110_direction_input;
374 	gpio->chip.direction_output	= aw9110_direction_output;
375 	gpio->chip.ngpio		= id->driver_data;
376 
377 	gpio->write	= aw9110_i2c_write_le8;
378 	gpio->read	= aw9110_i2c_read_le8;
379 
380 	gpio->chip.label = client->name;
381 
382 	gpio->client = client;
383 	i2c_set_clientdata(client, gpio);
384 
385 	status = aw9110_check_dev_id(client);
386 	if (status < 0) {
387 		dev_err(&client->dev, "check device id fail(%d)\n", status);
388 		goto fail;
389 	}
390 
391 	aw9110_state_init(gpio);
392 
393 	/* Enable irqchip if we have an interrupt */
394 	if (client->irq) {
395 		struct gpio_irq_chip *girq;
396 
397 		gpio->irqchip.name = "aw9110";
398 		gpio->irqchip.irq_enable = aw9110_irq_enable;
399 		gpio->irqchip.irq_disable = aw9110_irq_disable;
400 		gpio->irqchip.irq_ack = aw9110_noop;
401 		gpio->irqchip.irq_mask = aw9110_noop;
402 		gpio->irqchip.irq_unmask = aw9110_noop;
403 		gpio->irqchip.irq_set_wake = aw9110_irq_set_wake;
404 		gpio->irqchip.irq_bus_lock = aw9110_irq_bus_lock;
405 		gpio->irqchip.irq_bus_sync_unlock = aw9110_irq_bus_sync_unlock;
406 
407 		status = devm_request_threaded_irq(&client->dev, client->irq,
408 					NULL, aw9110_irq, IRQF_ONESHOT |
409 					IRQF_TRIGGER_FALLING | IRQF_SHARED,
410 					dev_name(&client->dev), gpio);
411 		if (status)
412 			goto fail;
413 
414 		girq = &gpio->chip.irq;
415 		girq->chip = &gpio->irqchip;
416 		/* This will let us handle the parent IRQ in the driver */
417 		girq->parent_handler = NULL;
418 		girq->num_parents = 0;
419 		girq->parents = NULL;
420 		girq->default_type = IRQ_TYPE_NONE;
421 		girq->handler = handle_level_irq;
422 		girq->threaded = true;
423 	}
424 
425 	status = devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
426 	if (status < 0)
427 		goto fail;
428 
429 	dev_info(&client->dev, "probed\n");
430 
431 	return 0;
432 
433 fail:
434 	dev_err(&client->dev, "probe error %d for '%s'\n", status,
435 		client->name);
436 
437 	return status;
438 }
439 
aw9110_pm_resume(struct device * dev)440 static int aw9110_pm_resume(struct device *dev)
441 {
442 	struct aw9110 *gpio = dev->driver_data;
443 
444 	/* out4-9 push-pull */
445 	gpio->write(gpio->client, REG_CTRL, (1<<4));
446 
447 	/* work mode : gpio */
448 	gpio->write(gpio->client, REG_WORK_MODE_P1, 0x0F);
449 	gpio->write(gpio->client, REG_WORK_MODE_P0, 0x3F);
450 
451 	/* direct */
452 	//gpio->direct = 0x03FF;	/* 0: output, 1:input */
453 	gpio->write(gpio->client, REG_CONFIG_P1, gpio->direct & 0x0F);
454 	gpio->write(gpio->client, REG_CONFIG_P0, (gpio->direct>>4) & 0x3F);
455 
456 	/* out */
457 	gpio->write(gpio->client, REG_OUTPUT_P1, gpio->out >> 0);
458 	gpio->write(gpio->client, REG_OUTPUT_P0, gpio->out >> 4);
459 
460 	/* interrupt enable */
461 	//gpio->irq_enabled = 0x03FF;	/* 0: disable 1:enable, chip: 0:enable,  1: disable */
462 	gpio->write(gpio->client, REG_INT_P1, ((~gpio->irq_enabled) >> 0)&0x0F);
463 	gpio->write(gpio->client, REG_INT_P0, ((~gpio->irq_enabled) >> 4)&0x3F);
464 
465 	return 0;
466 }
467 
468 static const struct dev_pm_ops aw9110_pm_ops = {
469 	.resume = aw9110_pm_resume,
470 };
471 
472 static struct i2c_driver aw9110_driver = {
473 	.driver = {
474 		.name	= "aw9110",
475 		.pm = &aw9110_pm_ops,
476 		.of_match_table = of_match_ptr(aw9110_of_table),
477 	},
478 	.probe	= aw9110_probe,
479 	.id_table = aw9110_id,
480 };
481 
aw9110_init(void)482 static int __init aw9110_init(void)
483 {
484 	return i2c_add_driver(&aw9110_driver);
485 }
486 /* register after i2c postcore initcall and before
487  * subsys initcalls that may rely on these GPIOs
488  */
489 subsys_initcall(aw9110_init);
490 
aw9110_exit(void)491 static void __exit aw9110_exit(void)
492 {
493 	i2c_del_driver(&aw9110_driver);
494 }
495 module_exit(aw9110_exit);
496 
497 MODULE_LICENSE("GPL");
498 MODULE_AUTHOR("Jake Wu <jake.wu@rock-chips.com>");
499 MODULE_DESCRIPTION("AW9110 i2c expander gpio driver");
500 
501