2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License, version 2
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/mfd/core.h>
17 #include <linux/mfd/tc3589x.h>
19 #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
20 #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
23 * tc3589x_reg_read() - read a single TC3589x register
24 * @tc3589x: Device to read from
25 * @reg: Register to read
27 int tc3589x_reg_read(struct tc3589x
*tc3589x
, u8 reg
)
31 ret
= i2c_smbus_read_byte_data(tc3589x
->i2c
, reg
);
33 dev_err(tc3589x
->dev
, "failed to read reg %#x: %d\n",
38 EXPORT_SYMBOL_GPL(tc3589x_reg_read
);
41 * tc3589x_reg_read() - write a single TC3589x register
42 * @tc3589x: Device to write to
43 * @reg: Register to read
44 * @data: Value to write
46 int tc3589x_reg_write(struct tc3589x
*tc3589x
, u8 reg
, u8 data
)
50 ret
= i2c_smbus_write_byte_data(tc3589x
->i2c
, reg
, data
);
52 dev_err(tc3589x
->dev
, "failed to write reg %#x: %d\n",
57 EXPORT_SYMBOL_GPL(tc3589x_reg_write
);
60 * tc3589x_block_read() - read multiple TC3589x registers
61 * @tc3589x: Device to read from
62 * @reg: First register
63 * @length: Number of registers
64 * @values: Buffer to write to
66 int tc3589x_block_read(struct tc3589x
*tc3589x
, u8 reg
, u8 length
, u8
*values
)
70 ret
= i2c_smbus_read_i2c_block_data(tc3589x
->i2c
, reg
, length
, values
);
72 dev_err(tc3589x
->dev
, "failed to read regs %#x: %d\n",
77 EXPORT_SYMBOL_GPL(tc3589x_block_read
);
80 * tc3589x_block_write() - write multiple TC3589x registers
81 * @tc3589x: Device to write to
82 * @reg: First register
83 * @length: Number of registers
84 * @values: Values to write
86 int tc3589x_block_write(struct tc3589x
*tc3589x
, u8 reg
, u8 length
,
91 ret
= i2c_smbus_write_i2c_block_data(tc3589x
->i2c
, reg
, length
,
94 dev_err(tc3589x
->dev
, "failed to write regs %#x: %d\n",
99 EXPORT_SYMBOL_GPL(tc3589x_block_write
);
102 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
103 * @tc3589x: Device to write to
104 * @reg: Register to write
105 * @mask: Mask of bits to set
106 * @values: Value to set
108 int tc3589x_set_bits(struct tc3589x
*tc3589x
, u8 reg
, u8 mask
, u8 val
)
112 mutex_lock(&tc3589x
->lock
);
114 ret
= tc3589x_reg_read(tc3589x
, reg
);
121 ret
= tc3589x_reg_write(tc3589x
, reg
, ret
);
124 mutex_unlock(&tc3589x
->lock
);
127 EXPORT_SYMBOL_GPL(tc3589x_set_bits
);
129 static struct resource gpio_resources
[] = {
131 .start
= TC3589x_INT_GPIIRQ
,
132 .end
= TC3589x_INT_GPIIRQ
,
133 .flags
= IORESOURCE_IRQ
,
137 static struct resource keypad_resources
[] = {
139 .start
= TC3589x_INT_KBDIRQ
,
140 .end
= TC3589x_INT_KBDIRQ
,
141 .flags
= IORESOURCE_IRQ
,
145 static struct mfd_cell tc3589x_dev_gpio
[] = {
147 .name
= "tc3589x-gpio",
148 .num_resources
= ARRAY_SIZE(gpio_resources
),
149 .resources
= &gpio_resources
[0],
150 .of_compatible
= "tc3589x-gpio",
154 static struct mfd_cell tc3589x_dev_keypad
[] = {
156 .name
= "tc3589x-keypad",
157 .num_resources
= ARRAY_SIZE(keypad_resources
),
158 .resources
= &keypad_resources
[0],
159 .of_compatible
= "tc3589x-keypad",
163 static irqreturn_t
tc3589x_irq(int irq
, void *data
)
165 struct tc3589x
*tc3589x
= data
;
169 status
= tc3589x_reg_read(tc3589x
, TC3589x_IRQST
);
174 int bit
= __ffs(status
);
175 int virq
= irq_create_mapping(tc3589x
->domain
, bit
);
177 handle_nested_irq(virq
);
178 status
&= ~(1 << bit
);
182 * A dummy read or write (to any register) appears to be necessary to
183 * have the last interrupt clear (for example, GPIO IC write) take
184 * effect. In such a case, recheck for any interrupt which is still
187 status
= tc3589x_reg_read(tc3589x
, TC3589x_IRQST
);
194 static int tc3589x_irq_map(struct irq_domain
*d
, unsigned int virq
,
195 irq_hw_number_t hwirq
)
197 struct tc3589x
*tc3589x
= d
->host_data
;
199 irq_set_chip_data(virq
, tc3589x
);
200 irq_set_chip_and_handler(virq
, &dummy_irq_chip
,
202 irq_set_nested_thread(virq
, 1);
204 set_irq_flags(virq
, IRQF_VALID
);
206 irq_set_noprobe(virq
);
212 static void tc3589x_irq_unmap(struct irq_domain
*d
, unsigned int virq
)
215 set_irq_flags(virq
, 0);
217 irq_set_chip_and_handler(virq
, NULL
, NULL
);
218 irq_set_chip_data(virq
, NULL
);
221 static struct irq_domain_ops tc3589x_irq_ops
= {
222 .map
= tc3589x_irq_map
,
223 .unmap
= tc3589x_irq_unmap
,
224 .xlate
= irq_domain_xlate_twocell
,
227 static int tc3589x_irq_init(struct tc3589x
*tc3589x
, struct device_node
*np
)
229 int base
= tc3589x
->irq_base
;
231 tc3589x
->domain
= irq_domain_add_simple(
232 np
, TC3589x_NR_INTERNAL_IRQS
, base
,
233 &tc3589x_irq_ops
, tc3589x
);
235 if (!tc3589x
->domain
) {
236 dev_err(tc3589x
->dev
, "Failed to create irqdomain\n");
243 static int tc3589x_chip_init(struct tc3589x
*tc3589x
)
247 manf
= tc3589x_reg_read(tc3589x
, TC3589x_MANFCODE
);
251 ver
= tc3589x_reg_read(tc3589x
, TC3589x_VERSION
);
255 if (manf
!= TC3589x_MANFCODE_MAGIC
) {
256 dev_err(tc3589x
->dev
, "unknown manufacturer: %#x\n", manf
);
260 dev_info(tc3589x
->dev
, "manufacturer: %#x, version: %#x\n", manf
, ver
);
263 * Put everything except the IRQ module into reset;
264 * also spare the GPIO module for any pin initialization
265 * done during pre-kernel boot
267 ret
= tc3589x_reg_write(tc3589x
, TC3589x_RSTCTRL
,
268 TC3589x_RSTCTRL_TIMRST
269 | TC3589x_RSTCTRL_ROTRST
270 | TC3589x_RSTCTRL_KBDRST
);
274 /* Clear the reset interrupt. */
275 return tc3589x_reg_write(tc3589x
, TC3589x_RSTINTCLR
, 0x1);
278 static int tc3589x_device_init(struct tc3589x
*tc3589x
)
281 unsigned int blocks
= tc3589x
->pdata
->block
;
283 if (blocks
& TC3589x_BLOCK_GPIO
) {
284 ret
= mfd_add_devices(tc3589x
->dev
, -1, tc3589x_dev_gpio
,
285 ARRAY_SIZE(tc3589x_dev_gpio
), NULL
,
286 tc3589x
->irq_base
, tc3589x
->domain
);
288 dev_err(tc3589x
->dev
, "failed to add gpio child\n");
291 dev_info(tc3589x
->dev
, "added gpio block\n");
294 if (blocks
& TC3589x_BLOCK_KEYPAD
) {
295 ret
= mfd_add_devices(tc3589x
->dev
, -1, tc3589x_dev_keypad
,
296 ARRAY_SIZE(tc3589x_dev_keypad
), NULL
,
297 tc3589x
->irq_base
, tc3589x
->domain
);
299 dev_err(tc3589x
->dev
, "failed to keypad child\n");
302 dev_info(tc3589x
->dev
, "added keypad block\n");
308 static int tc3589x_of_probe(struct device_node
*np
,
309 struct tc3589x_platform_data
*pdata
)
311 struct device_node
*child
;
313 for_each_child_of_node(np
, child
) {
314 if (!strcmp(child
->name
, "tc3589x_gpio")) {
315 pdata
->block
|= TC3589x_BLOCK_GPIO
;
317 if (!strcmp(child
->name
, "tc3589x_keypad")) {
318 pdata
->block
|= TC3589x_BLOCK_KEYPAD
;
325 static int tc3589x_probe(struct i2c_client
*i2c
,
326 const struct i2c_device_id
*id
)
328 struct tc3589x_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
329 struct device_node
*np
= i2c
->dev
.of_node
;
330 struct tc3589x
*tc3589x
;
335 pdata
= devm_kzalloc(&i2c
->dev
, sizeof(*pdata
), GFP_KERNEL
);
339 ret
= tc3589x_of_probe(np
, pdata
);
344 dev_err(&i2c
->dev
, "No platform data or DT found\n");
349 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
350 | I2C_FUNC_SMBUS_I2C_BLOCK
))
353 tc3589x
= devm_kzalloc(&i2c
->dev
, sizeof(struct tc3589x
),
358 mutex_init(&tc3589x
->lock
);
360 tc3589x
->dev
= &i2c
->dev
;
362 tc3589x
->pdata
= pdata
;
363 tc3589x
->irq_base
= pdata
->irq_base
;
364 tc3589x
->num_gpio
= id
->driver_data
;
366 i2c_set_clientdata(i2c
, tc3589x
);
368 ret
= tc3589x_chip_init(tc3589x
);
372 ret
= tc3589x_irq_init(tc3589x
, np
);
376 ret
= request_threaded_irq(tc3589x
->i2c
->irq
, NULL
, tc3589x_irq
,
377 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
380 dev_err(tc3589x
->dev
, "failed to request IRQ: %d\n", ret
);
384 ret
= tc3589x_device_init(tc3589x
);
386 dev_err(tc3589x
->dev
, "failed to add child devices\n");
393 static int tc3589x_remove(struct i2c_client
*client
)
395 struct tc3589x
*tc3589x
= i2c_get_clientdata(client
);
397 mfd_remove_devices(tc3589x
->dev
);
402 #ifdef CONFIG_PM_SLEEP
403 static int tc3589x_suspend(struct device
*dev
)
405 struct tc3589x
*tc3589x
= dev_get_drvdata(dev
);
406 struct i2c_client
*client
= tc3589x
->i2c
;
409 /* put the system to sleep mode */
410 if (!device_may_wakeup(&client
->dev
))
411 ret
= tc3589x_reg_write(tc3589x
, TC3589x_CLKMODE
,
412 TC3589x_CLKMODE_MODCTL_SLEEP
);
417 static int tc3589x_resume(struct device
*dev
)
419 struct tc3589x
*tc3589x
= dev_get_drvdata(dev
);
420 struct i2c_client
*client
= tc3589x
->i2c
;
423 /* enable the system into operation */
424 if (!device_may_wakeup(&client
->dev
))
425 ret
= tc3589x_reg_write(tc3589x
, TC3589x_CLKMODE
,
426 TC3589x_CLKMODE_MODCTL_OPERATION
);
432 static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops
, tc3589x_suspend
, tc3589x_resume
);
434 static const struct i2c_device_id tc3589x_id
[] = {
438 MODULE_DEVICE_TABLE(i2c
, tc3589x_id
);
440 static struct i2c_driver tc3589x_driver
= {
441 .driver
.name
= "tc3589x",
442 .driver
.owner
= THIS_MODULE
,
443 .driver
.pm
= &tc3589x_dev_pm_ops
,
444 .probe
= tc3589x_probe
,
445 .remove
= tc3589x_remove
,
446 .id_table
= tc3589x_id
,
449 static int __init
tc3589x_init(void)
451 return i2c_add_driver(&tc3589x_driver
);
453 subsys_initcall(tc3589x_init
);
455 static void __exit
tc3589x_exit(void)
457 i2c_del_driver(&tc3589x_driver
);
459 module_exit(tc3589x_exit
);
461 MODULE_LICENSE("GPL v2");
462 MODULE_DESCRIPTION("TC3589x MFD core driver");
463 MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");