1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
6 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
16 #include <linux/of_device.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/tc3589x.h>
19 #include <linux/err.h>
22 * enum tc3589x_version - indicates the TC3589x version
24 enum tc3589x_version
{
34 #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
35 #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
38 * tc3589x_reg_read() - read a single TC3589x register
39 * @tc3589x: Device to read from
40 * @reg: Register to read
42 int tc3589x_reg_read(struct tc3589x
*tc3589x
, u8 reg
)
46 ret
= i2c_smbus_read_byte_data(tc3589x
->i2c
, reg
);
48 dev_err(tc3589x
->dev
, "failed to read reg %#x: %d\n",
53 EXPORT_SYMBOL_GPL(tc3589x_reg_read
);
56 * tc3589x_reg_write() - write a single TC3589x register
57 * @tc3589x: Device to write to
58 * @reg: Register to read
59 * @data: Value to write
61 int tc3589x_reg_write(struct tc3589x
*tc3589x
, u8 reg
, u8 data
)
65 ret
= i2c_smbus_write_byte_data(tc3589x
->i2c
, reg
, data
);
67 dev_err(tc3589x
->dev
, "failed to write reg %#x: %d\n",
72 EXPORT_SYMBOL_GPL(tc3589x_reg_write
);
75 * tc3589x_block_read() - read multiple TC3589x registers
76 * @tc3589x: Device to read from
77 * @reg: First register
78 * @length: Number of registers
79 * @values: Buffer to write to
81 int tc3589x_block_read(struct tc3589x
*tc3589x
, u8 reg
, u8 length
, u8
*values
)
85 ret
= i2c_smbus_read_i2c_block_data(tc3589x
->i2c
, reg
, length
, values
);
87 dev_err(tc3589x
->dev
, "failed to read regs %#x: %d\n",
92 EXPORT_SYMBOL_GPL(tc3589x_block_read
);
95 * tc3589x_block_write() - write multiple TC3589x registers
96 * @tc3589x: Device to write to
97 * @reg: First register
98 * @length: Number of registers
99 * @values: Values to write
101 int tc3589x_block_write(struct tc3589x
*tc3589x
, u8 reg
, u8 length
,
106 ret
= i2c_smbus_write_i2c_block_data(tc3589x
->i2c
, reg
, length
,
109 dev_err(tc3589x
->dev
, "failed to write regs %#x: %d\n",
114 EXPORT_SYMBOL_GPL(tc3589x_block_write
);
117 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
118 * @tc3589x: Device to write to
119 * @reg: Register to write
120 * @mask: Mask of bits to set
123 int tc3589x_set_bits(struct tc3589x
*tc3589x
, u8 reg
, u8 mask
, u8 val
)
127 mutex_lock(&tc3589x
->lock
);
129 ret
= tc3589x_reg_read(tc3589x
, reg
);
136 ret
= tc3589x_reg_write(tc3589x
, reg
, ret
);
139 mutex_unlock(&tc3589x
->lock
);
142 EXPORT_SYMBOL_GPL(tc3589x_set_bits
);
144 static const struct resource gpio_resources
[] = {
146 .start
= TC3589x_INT_GPIIRQ
,
147 .end
= TC3589x_INT_GPIIRQ
,
148 .flags
= IORESOURCE_IRQ
,
152 static const struct resource keypad_resources
[] = {
154 .start
= TC3589x_INT_KBDIRQ
,
155 .end
= TC3589x_INT_KBDIRQ
,
156 .flags
= IORESOURCE_IRQ
,
160 static const struct mfd_cell tc3589x_dev_gpio
[] = {
162 .name
= "tc3589x-gpio",
163 .num_resources
= ARRAY_SIZE(gpio_resources
),
164 .resources
= &gpio_resources
[0],
165 .of_compatible
= "toshiba,tc3589x-gpio",
169 static const struct mfd_cell tc3589x_dev_keypad
[] = {
171 .name
= "tc3589x-keypad",
172 .num_resources
= ARRAY_SIZE(keypad_resources
),
173 .resources
= &keypad_resources
[0],
174 .of_compatible
= "toshiba,tc3589x-keypad",
178 static irqreturn_t
tc3589x_irq(int irq
, void *data
)
180 struct tc3589x
*tc3589x
= data
;
184 status
= tc3589x_reg_read(tc3589x
, TC3589x_IRQST
);
189 int bit
= __ffs(status
);
190 int virq
= irq_find_mapping(tc3589x
->domain
, bit
);
192 handle_nested_irq(virq
);
193 status
&= ~(1 << bit
);
197 * A dummy read or write (to any register) appears to be necessary to
198 * have the last interrupt clear (for example, GPIO IC write) take
199 * effect. In such a case, recheck for any interrupt which is still
202 status
= tc3589x_reg_read(tc3589x
, TC3589x_IRQST
);
209 static int tc3589x_irq_map(struct irq_domain
*d
, unsigned int virq
,
210 irq_hw_number_t hwirq
)
212 struct tc3589x
*tc3589x
= d
->host_data
;
214 irq_set_chip_data(virq
, tc3589x
);
215 irq_set_chip_and_handler(virq
, &dummy_irq_chip
,
217 irq_set_nested_thread(virq
, 1);
218 irq_set_noprobe(virq
);
223 static void tc3589x_irq_unmap(struct irq_domain
*d
, unsigned int virq
)
225 irq_set_chip_and_handler(virq
, NULL
, NULL
);
226 irq_set_chip_data(virq
, NULL
);
229 static const struct irq_domain_ops tc3589x_irq_ops
= {
230 .map
= tc3589x_irq_map
,
231 .unmap
= tc3589x_irq_unmap
,
232 .xlate
= irq_domain_xlate_onecell
,
235 static int tc3589x_irq_init(struct tc3589x
*tc3589x
, struct device_node
*np
)
237 tc3589x
->domain
= irq_domain_add_simple(
238 np
, TC3589x_NR_INTERNAL_IRQS
, 0,
239 &tc3589x_irq_ops
, tc3589x
);
241 if (!tc3589x
->domain
) {
242 dev_err(tc3589x
->dev
, "Failed to create irqdomain\n");
249 static int tc3589x_chip_init(struct tc3589x
*tc3589x
)
253 manf
= tc3589x_reg_read(tc3589x
, TC3589x_MANFCODE
);
257 ver
= tc3589x_reg_read(tc3589x
, TC3589x_VERSION
);
261 if (manf
!= TC3589x_MANFCODE_MAGIC
) {
262 dev_err(tc3589x
->dev
, "unknown manufacturer: %#x\n", manf
);
266 dev_info(tc3589x
->dev
, "manufacturer: %#x, version: %#x\n", manf
, ver
);
269 * Put everything except the IRQ module into reset;
270 * also spare the GPIO module for any pin initialization
271 * done during pre-kernel boot
273 ret
= tc3589x_reg_write(tc3589x
, TC3589x_RSTCTRL
,
274 TC3589x_RSTCTRL_TIMRST
275 | TC3589x_RSTCTRL_ROTRST
276 | TC3589x_RSTCTRL_KBDRST
);
280 /* Clear the reset interrupt. */
281 return tc3589x_reg_write(tc3589x
, TC3589x_RSTINTCLR
, 0x1);
284 static int tc3589x_device_init(struct tc3589x
*tc3589x
)
287 unsigned int blocks
= tc3589x
->pdata
->block
;
289 if (blocks
& TC3589x_BLOCK_GPIO
) {
290 ret
= mfd_add_devices(tc3589x
->dev
, -1, tc3589x_dev_gpio
,
291 ARRAY_SIZE(tc3589x_dev_gpio
), NULL
,
294 dev_err(tc3589x
->dev
, "failed to add gpio child\n");
297 dev_info(tc3589x
->dev
, "added gpio block\n");
300 if (blocks
& TC3589x_BLOCK_KEYPAD
) {
301 ret
= mfd_add_devices(tc3589x
->dev
, -1, tc3589x_dev_keypad
,
302 ARRAY_SIZE(tc3589x_dev_keypad
), NULL
,
305 dev_err(tc3589x
->dev
, "failed to keypad child\n");
308 dev_info(tc3589x
->dev
, "added keypad block\n");
314 static const struct of_device_id tc3589x_match
[] = {
315 { .compatible
= "toshiba,tc35890", .data
= (void *) TC3589X_TC35890
},
316 { .compatible
= "toshiba,tc35892", .data
= (void *) TC3589X_TC35892
},
317 { .compatible
= "toshiba,tc35893", .data
= (void *) TC3589X_TC35893
},
318 { .compatible
= "toshiba,tc35894", .data
= (void *) TC3589X_TC35894
},
319 { .compatible
= "toshiba,tc35895", .data
= (void *) TC3589X_TC35895
},
320 { .compatible
= "toshiba,tc35896", .data
= (void *) TC3589X_TC35896
},
324 MODULE_DEVICE_TABLE(of
, tc3589x_match
);
326 static struct tc3589x_platform_data
*
327 tc3589x_of_probe(struct device
*dev
, enum tc3589x_version
*version
)
329 struct device_node
*np
= dev
->of_node
;
330 struct tc3589x_platform_data
*pdata
;
331 struct device_node
*child
;
332 const struct of_device_id
*of_id
;
334 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
336 return ERR_PTR(-ENOMEM
);
338 of_id
= of_match_device(tc3589x_match
, dev
);
340 return ERR_PTR(-ENODEV
);
341 *version
= (uintptr_t) of_id
->data
;
343 for_each_child_of_node(np
, child
) {
344 if (of_device_is_compatible(child
, "toshiba,tc3589x-gpio"))
345 pdata
->block
|= TC3589x_BLOCK_GPIO
;
346 if (of_device_is_compatible(child
, "toshiba,tc3589x-keypad"))
347 pdata
->block
|= TC3589x_BLOCK_KEYPAD
;
353 static int tc3589x_probe(struct i2c_client
*i2c
)
355 const struct i2c_device_id
*id
= i2c_client_get_device_id(i2c
);
356 struct device_node
*np
= i2c
->dev
.of_node
;
357 struct tc3589x_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
358 struct tc3589x
*tc3589x
;
359 enum tc3589x_version version
;
363 pdata
= tc3589x_of_probe(&i2c
->dev
, &version
);
365 dev_err(&i2c
->dev
, "No platform data or DT found\n");
366 return PTR_ERR(pdata
);
369 /* When not probing from device tree we have this ID */
370 version
= id
->driver_data
;
373 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
374 | I2C_FUNC_SMBUS_I2C_BLOCK
))
377 tc3589x
= devm_kzalloc(&i2c
->dev
, sizeof(struct tc3589x
),
382 mutex_init(&tc3589x
->lock
);
384 tc3589x
->dev
= &i2c
->dev
;
386 tc3589x
->pdata
= pdata
;
389 case TC3589X_TC35893
:
390 case TC3589X_TC35895
:
391 case TC3589X_TC35896
:
392 tc3589x
->num_gpio
= 20;
394 case TC3589X_TC35890
:
395 case TC3589X_TC35892
:
396 case TC3589X_TC35894
:
397 case TC3589X_UNKNOWN
:
399 tc3589x
->num_gpio
= 24;
403 i2c_set_clientdata(i2c
, tc3589x
);
405 ret
= tc3589x_chip_init(tc3589x
);
409 ret
= tc3589x_irq_init(tc3589x
, np
);
413 ret
= request_threaded_irq(tc3589x
->i2c
->irq
, NULL
, tc3589x_irq
,
414 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
417 dev_err(tc3589x
->dev
, "failed to request IRQ: %d\n", ret
);
421 ret
= tc3589x_device_init(tc3589x
);
423 dev_err(tc3589x
->dev
, "failed to add child devices\n");
430 static void tc3589x_remove(struct i2c_client
*client
)
432 struct tc3589x
*tc3589x
= i2c_get_clientdata(client
);
434 mfd_remove_devices(tc3589x
->dev
);
437 static int tc3589x_suspend(struct device
*dev
)
439 struct tc3589x
*tc3589x
= dev_get_drvdata(dev
);
440 struct i2c_client
*client
= tc3589x
->i2c
;
443 /* put the system to sleep mode */
444 if (!device_may_wakeup(&client
->dev
))
445 ret
= tc3589x_reg_write(tc3589x
, TC3589x_CLKMODE
,
446 TC3589x_CLKMODE_MODCTL_SLEEP
);
451 static int tc3589x_resume(struct device
*dev
)
453 struct tc3589x
*tc3589x
= dev_get_drvdata(dev
);
454 struct i2c_client
*client
= tc3589x
->i2c
;
457 /* enable the system into operation */
458 if (!device_may_wakeup(&client
->dev
))
459 ret
= tc3589x_reg_write(tc3589x
, TC3589x_CLKMODE
,
460 TC3589x_CLKMODE_MODCTL_OPERATION
);
465 static DEFINE_SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops
,
466 tc3589x_suspend
, tc3589x_resume
);
468 static const struct i2c_device_id tc3589x_id
[] = {
469 { "tc35890", TC3589X_TC35890
},
470 { "tc35892", TC3589X_TC35892
},
471 { "tc35893", TC3589X_TC35893
},
472 { "tc35894", TC3589X_TC35894
},
473 { "tc35895", TC3589X_TC35895
},
474 { "tc35896", TC3589X_TC35896
},
475 { "tc3589x", TC3589X_UNKNOWN
},
478 MODULE_DEVICE_TABLE(i2c
, tc3589x_id
);
480 static struct i2c_driver tc3589x_driver
= {
483 .pm
= pm_sleep_ptr(&tc3589x_dev_pm_ops
),
484 .of_match_table
= tc3589x_match
,
486 .probe
= tc3589x_probe
,
487 .remove
= tc3589x_remove
,
488 .id_table
= tc3589x_id
,
491 static int __init
tc3589x_init(void)
493 return i2c_add_driver(&tc3589x_driver
);
495 subsys_initcall(tc3589x_init
);
497 static void __exit
tc3589x_exit(void)
499 i2c_del_driver(&tc3589x_driver
);
501 module_exit(tc3589x_exit
);
503 MODULE_DESCRIPTION("TC3589x MFD core driver");
504 MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");