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/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/mfd/core.h>
15 #include <linux/mfd/tc3589x.h>
17 #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
18 #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
21 * tc3589x_reg_read() - read a single TC3589x register
22 * @tc3589x: Device to read from
23 * @reg: Register to read
25 int tc3589x_reg_read(struct tc3589x
*tc3589x
, u8 reg
)
29 ret
= i2c_smbus_read_byte_data(tc3589x
->i2c
, reg
);
31 dev_err(tc3589x
->dev
, "failed to read reg %#x: %d\n",
36 EXPORT_SYMBOL_GPL(tc3589x_reg_read
);
39 * tc3589x_reg_read() - write a single TC3589x register
40 * @tc3589x: Device to write to
41 * @reg: Register to read
42 * @data: Value to write
44 int tc3589x_reg_write(struct tc3589x
*tc3589x
, u8 reg
, u8 data
)
48 ret
= i2c_smbus_write_byte_data(tc3589x
->i2c
, reg
, data
);
50 dev_err(tc3589x
->dev
, "failed to write reg %#x: %d\n",
55 EXPORT_SYMBOL_GPL(tc3589x_reg_write
);
58 * tc3589x_block_read() - read multiple TC3589x registers
59 * @tc3589x: Device to read from
60 * @reg: First register
61 * @length: Number of registers
62 * @values: Buffer to write to
64 int tc3589x_block_read(struct tc3589x
*tc3589x
, u8 reg
, u8 length
, u8
*values
)
68 ret
= i2c_smbus_read_i2c_block_data(tc3589x
->i2c
, reg
, length
, values
);
70 dev_err(tc3589x
->dev
, "failed to read regs %#x: %d\n",
75 EXPORT_SYMBOL_GPL(tc3589x_block_read
);
78 * tc3589x_block_write() - write multiple TC3589x registers
79 * @tc3589x: Device to write to
80 * @reg: First register
81 * @length: Number of registers
82 * @values: Values to write
84 int tc3589x_block_write(struct tc3589x
*tc3589x
, u8 reg
, u8 length
,
89 ret
= i2c_smbus_write_i2c_block_data(tc3589x
->i2c
, reg
, length
,
92 dev_err(tc3589x
->dev
, "failed to write regs %#x: %d\n",
97 EXPORT_SYMBOL_GPL(tc3589x_block_write
);
100 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
101 * @tc3589x: Device to write to
102 * @reg: Register to write
103 * @mask: Mask of bits to set
104 * @values: Value to set
106 int tc3589x_set_bits(struct tc3589x
*tc3589x
, u8 reg
, u8 mask
, u8 val
)
110 mutex_lock(&tc3589x
->lock
);
112 ret
= tc3589x_reg_read(tc3589x
, reg
);
119 ret
= tc3589x_reg_write(tc3589x
, reg
, ret
);
122 mutex_unlock(&tc3589x
->lock
);
125 EXPORT_SYMBOL_GPL(tc3589x_set_bits
);
127 static struct resource gpio_resources
[] = {
129 .start
= TC3589x_INT_GPIIRQ
,
130 .end
= TC3589x_INT_GPIIRQ
,
131 .flags
= IORESOURCE_IRQ
,
135 static struct resource keypad_resources
[] = {
137 .start
= TC3589x_INT_KBDIRQ
,
138 .end
= TC3589x_INT_KBDIRQ
,
139 .flags
= IORESOURCE_IRQ
,
143 static struct mfd_cell tc3589x_dev_gpio
[] = {
145 .name
= "tc3589x-gpio",
146 .num_resources
= ARRAY_SIZE(gpio_resources
),
147 .resources
= &gpio_resources
[0],
151 static struct mfd_cell tc3589x_dev_keypad
[] = {
153 .name
= "tc3589x-keypad",
154 .num_resources
= ARRAY_SIZE(keypad_resources
),
155 .resources
= &keypad_resources
[0],
159 static irqreturn_t
tc3589x_irq(int irq
, void *data
)
161 struct tc3589x
*tc3589x
= data
;
165 status
= tc3589x_reg_read(tc3589x
, TC3589x_IRQST
);
170 int bit
= __ffs(status
);
172 handle_nested_irq(tc3589x
->irq_base
+ bit
);
173 status
&= ~(1 << bit
);
177 * A dummy read or write (to any register) appears to be necessary to
178 * have the last interrupt clear (for example, GPIO IC write) take
179 * effect. In such a case, recheck for any interrupt which is still
182 status
= tc3589x_reg_read(tc3589x
, TC3589x_IRQST
);
189 static int tc3589x_irq_init(struct tc3589x
*tc3589x
)
191 int base
= tc3589x
->irq_base
;
194 for (irq
= base
; irq
< base
+ TC3589x_NR_INTERNAL_IRQS
; irq
++) {
195 irq_set_chip_data(irq
, tc3589x
);
196 irq_set_chip_and_handler(irq
, &dummy_irq_chip
,
198 irq_set_nested_thread(irq
, 1);
200 set_irq_flags(irq
, IRQF_VALID
);
202 irq_set_noprobe(irq
);
209 static void tc3589x_irq_remove(struct tc3589x
*tc3589x
)
211 int base
= tc3589x
->irq_base
;
214 for (irq
= base
; irq
< base
+ TC3589x_NR_INTERNAL_IRQS
; irq
++) {
216 set_irq_flags(irq
, 0);
218 irq_set_chip_and_handler(irq
, NULL
, NULL
);
219 irq_set_chip_data(irq
, NULL
);
223 static int tc3589x_chip_init(struct tc3589x
*tc3589x
)
227 manf
= tc3589x_reg_read(tc3589x
, TC3589x_MANFCODE
);
231 ver
= tc3589x_reg_read(tc3589x
, TC3589x_VERSION
);
235 if (manf
!= TC3589x_MANFCODE_MAGIC
) {
236 dev_err(tc3589x
->dev
, "unknown manufacturer: %#x\n", manf
);
240 dev_info(tc3589x
->dev
, "manufacturer: %#x, version: %#x\n", manf
, ver
);
243 * Put everything except the IRQ module into reset;
244 * also spare the GPIO module for any pin initialization
245 * done during pre-kernel boot
247 ret
= tc3589x_reg_write(tc3589x
, TC3589x_RSTCTRL
,
248 TC3589x_RSTCTRL_TIMRST
249 | TC3589x_RSTCTRL_ROTRST
250 | TC3589x_RSTCTRL_KBDRST
);
254 /* Clear the reset interrupt. */
255 return tc3589x_reg_write(tc3589x
, TC3589x_RSTINTCLR
, 0x1);
258 static int __devinit
tc3589x_device_init(struct tc3589x
*tc3589x
)
261 unsigned int blocks
= tc3589x
->pdata
->block
;
263 if (blocks
& TC3589x_BLOCK_GPIO
) {
264 ret
= mfd_add_devices(tc3589x
->dev
, -1, tc3589x_dev_gpio
,
265 ARRAY_SIZE(tc3589x_dev_gpio
), NULL
,
268 dev_err(tc3589x
->dev
, "failed to add gpio child\n");
271 dev_info(tc3589x
->dev
, "added gpio block\n");
274 if (blocks
& TC3589x_BLOCK_KEYPAD
) {
275 ret
= mfd_add_devices(tc3589x
->dev
, -1, tc3589x_dev_keypad
,
276 ARRAY_SIZE(tc3589x_dev_keypad
), NULL
,
279 dev_err(tc3589x
->dev
, "failed to keypad child\n");
282 dev_info(tc3589x
->dev
, "added keypad block\n");
288 static int __devinit
tc3589x_probe(struct i2c_client
*i2c
,
289 const struct i2c_device_id
*id
)
291 struct tc3589x_platform_data
*pdata
= i2c
->dev
.platform_data
;
292 struct tc3589x
*tc3589x
;
295 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
296 | I2C_FUNC_SMBUS_I2C_BLOCK
))
299 tc3589x
= kzalloc(sizeof(struct tc3589x
), GFP_KERNEL
);
303 mutex_init(&tc3589x
->lock
);
305 tc3589x
->dev
= &i2c
->dev
;
307 tc3589x
->pdata
= pdata
;
308 tc3589x
->irq_base
= pdata
->irq_base
;
309 tc3589x
->num_gpio
= id
->driver_data
;
311 i2c_set_clientdata(i2c
, tc3589x
);
313 ret
= tc3589x_chip_init(tc3589x
);
317 ret
= tc3589x_irq_init(tc3589x
);
321 ret
= request_threaded_irq(tc3589x
->i2c
->irq
, NULL
, tc3589x_irq
,
322 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
325 dev_err(tc3589x
->dev
, "failed to request IRQ: %d\n", ret
);
329 ret
= tc3589x_device_init(tc3589x
);
331 dev_err(tc3589x
->dev
, "failed to add child devices\n");
338 free_irq(tc3589x
->i2c
->irq
, tc3589x
);
340 tc3589x_irq_remove(tc3589x
);
346 static int __devexit
tc3589x_remove(struct i2c_client
*client
)
348 struct tc3589x
*tc3589x
= i2c_get_clientdata(client
);
350 mfd_remove_devices(tc3589x
->dev
);
352 free_irq(tc3589x
->i2c
->irq
, tc3589x
);
353 tc3589x_irq_remove(tc3589x
);
361 static int tc3589x_suspend(struct device
*dev
)
363 struct tc3589x
*tc3589x
= dev_get_drvdata(dev
);
364 struct i2c_client
*client
= tc3589x
->i2c
;
367 /* put the system to sleep mode */
368 if (!device_may_wakeup(&client
->dev
))
369 ret
= tc3589x_reg_write(tc3589x
, TC3589x_CLKMODE
,
370 TC3589x_CLKMODE_MODCTL_SLEEP
);
375 static int tc3589x_resume(struct device
*dev
)
377 struct tc3589x
*tc3589x
= dev_get_drvdata(dev
);
378 struct i2c_client
*client
= tc3589x
->i2c
;
381 /* enable the system into operation */
382 if (!device_may_wakeup(&client
->dev
))
383 ret
= tc3589x_reg_write(tc3589x
, TC3589x_CLKMODE
,
384 TC3589x_CLKMODE_MODCTL_OPERATION
);
389 static const SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops
, tc3589x_suspend
,
393 static const struct i2c_device_id tc3589x_id
[] = {
397 MODULE_DEVICE_TABLE(i2c
, tc3589x_id
);
399 static struct i2c_driver tc3589x_driver
= {
400 .driver
.name
= "tc3589x",
401 .driver
.owner
= THIS_MODULE
,
403 .driver
.pm
= &tc3589x_dev_pm_ops
,
405 .probe
= tc3589x_probe
,
406 .remove
= __devexit_p(tc3589x_remove
),
407 .id_table
= tc3589x_id
,
410 static int __init
tc3589x_init(void)
412 return i2c_add_driver(&tc3589x_driver
);
414 subsys_initcall(tc3589x_init
);
416 static void __exit
tc3589x_exit(void)
418 i2c_del_driver(&tc3589x_driver
);
420 module_exit(tc3589x_exit
);
422 MODULE_LICENSE("GPL v2");
423 MODULE_DESCRIPTION("TC3589x MFD core driver");
424 MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");