1 /* NXP PCF50633 Power Management Unit (PMU) driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Harald Welte <laforge@openmoko.org>
5 * Balaji Rao <balajirrao@openmoko.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/sysfs.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/interrupt.h>
21 #include <linux/workqueue.h>
22 #include <linux/platform_device.h>
23 #include <linux/i2c.h>
24 #include <linux/irq.h>
26 #include <linux/mfd/pcf50633/core.h>
28 /* Two MBCS registers used during cold start */
29 #define PCF50633_REG_MBCS1 0x4b
30 #define PCF50633_REG_MBCS2 0x4c
31 #define PCF50633_MBCS1_USBPRES 0x01
32 #define PCF50633_MBCS1_ADAPTPRES 0x01
34 static int __pcf50633_read(struct pcf50633
*pcf
, u8 reg
, int num
, u8
*data
)
38 ret
= i2c_smbus_read_i2c_block_data(pcf
->i2c_client
, reg
,
41 dev_err(pcf
->dev
, "Error reading %d regs at %d\n", num
, reg
);
46 static int __pcf50633_write(struct pcf50633
*pcf
, u8 reg
, int num
, u8
*data
)
50 ret
= i2c_smbus_write_i2c_block_data(pcf
->i2c_client
, reg
,
53 dev_err(pcf
->dev
, "Error writing %d regs at %d\n", num
, reg
);
59 /* Read a block of upto 32 regs */
60 int pcf50633_read_block(struct pcf50633
*pcf
, u8 reg
,
61 int nr_regs
, u8
*data
)
65 mutex_lock(&pcf
->lock
);
66 ret
= __pcf50633_read(pcf
, reg
, nr_regs
, data
);
67 mutex_unlock(&pcf
->lock
);
71 EXPORT_SYMBOL_GPL(pcf50633_read_block
);
73 /* Write a block of upto 32 regs */
74 int pcf50633_write_block(struct pcf50633
*pcf
, u8 reg
,
75 int nr_regs
, u8
*data
)
79 mutex_lock(&pcf
->lock
);
80 ret
= __pcf50633_write(pcf
, reg
, nr_regs
, data
);
81 mutex_unlock(&pcf
->lock
);
85 EXPORT_SYMBOL_GPL(pcf50633_write_block
);
87 u8
pcf50633_reg_read(struct pcf50633
*pcf
, u8 reg
)
91 mutex_lock(&pcf
->lock
);
92 __pcf50633_read(pcf
, reg
, 1, &val
);
93 mutex_unlock(&pcf
->lock
);
97 EXPORT_SYMBOL_GPL(pcf50633_reg_read
);
99 int pcf50633_reg_write(struct pcf50633
*pcf
, u8 reg
, u8 val
)
103 mutex_lock(&pcf
->lock
);
104 ret
= __pcf50633_write(pcf
, reg
, 1, &val
);
105 mutex_unlock(&pcf
->lock
);
109 EXPORT_SYMBOL_GPL(pcf50633_reg_write
);
111 int pcf50633_reg_set_bit_mask(struct pcf50633
*pcf
, u8 reg
, u8 mask
, u8 val
)
118 mutex_lock(&pcf
->lock
);
119 ret
= __pcf50633_read(pcf
, reg
, 1, &tmp
);
125 ret
= __pcf50633_write(pcf
, reg
, 1, &tmp
);
128 mutex_unlock(&pcf
->lock
);
132 EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask
);
134 int pcf50633_reg_clear_bits(struct pcf50633
*pcf
, u8 reg
, u8 val
)
139 mutex_lock(&pcf
->lock
);
140 ret
= __pcf50633_read(pcf
, reg
, 1, &tmp
);
145 ret
= __pcf50633_write(pcf
, reg
, 1, &tmp
);
148 mutex_unlock(&pcf
->lock
);
152 EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits
);
154 /* sysfs attributes */
155 static ssize_t
show_dump_regs(struct device
*dev
, struct device_attribute
*attr
,
158 struct pcf50633
*pcf
= dev_get_drvdata(dev
);
162 static u8 address_no_read
[] = { /* must be ascending */
171 for (n
= 0; n
< 256; n
+= sizeof(dump
)) {
172 for (n1
= 0; n1
< sizeof(dump
); n1
++)
173 if (n
== address_no_read
[idx
]) {
177 dump
[n1
] = pcf50633_reg_read(pcf
, n
+ n1
);
179 hex_dump_to_buffer(dump
, sizeof(dump
), 16, 1, buf1
, 128, 0);
180 buf1
+= strlen(buf1
);
187 static DEVICE_ATTR(dump_regs
, 0400, show_dump_regs
, NULL
);
189 static ssize_t
show_resume_reason(struct device
*dev
,
190 struct device_attribute
*attr
, char *buf
)
192 struct pcf50633
*pcf
= dev_get_drvdata(dev
);
195 n
= sprintf(buf
, "%02x%02x%02x%02x%02x\n",
196 pcf
->resume_reason
[0],
197 pcf
->resume_reason
[1],
198 pcf
->resume_reason
[2],
199 pcf
->resume_reason
[3],
200 pcf
->resume_reason
[4]);
204 static DEVICE_ATTR(resume_reason
, 0400, show_resume_reason
, NULL
);
206 static struct attribute
*pcf_sysfs_entries
[] = {
207 &dev_attr_dump_regs
.attr
,
208 &dev_attr_resume_reason
.attr
,
212 static struct attribute_group pcf_attr_group
= {
213 .name
= NULL
, /* put in device directory */
214 .attrs
= pcf_sysfs_entries
,
217 int pcf50633_register_irq(struct pcf50633
*pcf
, int irq
,
218 void (*handler
) (int, void *), void *data
)
220 if (irq
< 0 || irq
> PCF50633_NUM_IRQ
|| !handler
)
223 if (WARN_ON(pcf
->irq_handler
[irq
].handler
))
226 mutex_lock(&pcf
->lock
);
227 pcf
->irq_handler
[irq
].handler
= handler
;
228 pcf
->irq_handler
[irq
].data
= data
;
229 mutex_unlock(&pcf
->lock
);
233 EXPORT_SYMBOL_GPL(pcf50633_register_irq
);
235 int pcf50633_free_irq(struct pcf50633
*pcf
, int irq
)
237 if (irq
< 0 || irq
> PCF50633_NUM_IRQ
)
240 mutex_lock(&pcf
->lock
);
241 pcf
->irq_handler
[irq
].handler
= NULL
;
242 mutex_unlock(&pcf
->lock
);
246 EXPORT_SYMBOL_GPL(pcf50633_free_irq
);
248 static int __pcf50633_irq_mask_set(struct pcf50633
*pcf
, int irq
, u8 mask
)
254 reg
= PCF50633_REG_INT1M
+ idx
;
255 bits
= 1 << (irq
& 0x07);
257 mutex_lock(&pcf
->lock
);
260 ret
= __pcf50633_read(pcf
, reg
, 1, &tmp
);
266 ret
= __pcf50633_write(pcf
, reg
, 1, &tmp
);
270 pcf
->mask_regs
[idx
] &= ~bits
;
271 pcf
->mask_regs
[idx
] |= bits
;
273 ret
= __pcf50633_read(pcf
, reg
, 1, &tmp
);
279 ret
= __pcf50633_write(pcf
, reg
, 1, &tmp
);
283 pcf
->mask_regs
[idx
] &= ~bits
;
286 mutex_unlock(&pcf
->lock
);
291 int pcf50633_irq_mask(struct pcf50633
*pcf
, int irq
)
293 dev_info(pcf
->dev
, "Masking IRQ %d\n", irq
);
295 return __pcf50633_irq_mask_set(pcf
, irq
, 1);
297 EXPORT_SYMBOL_GPL(pcf50633_irq_mask
);
299 int pcf50633_irq_unmask(struct pcf50633
*pcf
, int irq
)
301 dev_info(pcf
->dev
, "Unmasking IRQ %d\n", irq
);
303 return __pcf50633_irq_mask_set(pcf
, irq
, 0);
305 EXPORT_SYMBOL_GPL(pcf50633_irq_unmask
);
307 int pcf50633_irq_mask_get(struct pcf50633
*pcf
, int irq
)
312 bits
= 1 << (irq
& 0x07);
314 return pcf
->mask_regs
[reg
] & bits
;
316 EXPORT_SYMBOL_GPL(pcf50633_irq_mask_get
);
318 static void pcf50633_irq_call_handler(struct pcf50633
*pcf
, int irq
)
320 if (pcf
->irq_handler
[irq
].handler
)
321 pcf
->irq_handler
[irq
].handler(irq
, pcf
->irq_handler
[irq
].data
);
324 /* Maximum amount of time ONKEY is held before emergency action is taken */
325 #define PCF50633_ONKEY1S_TIMEOUT 8
327 static void pcf50633_irq_worker(struct work_struct
*work
)
329 struct pcf50633
*pcf
;
331 u8 pcf_int
[5], chgstat
;
333 pcf
= container_of(work
, struct pcf50633
, irq_work
);
335 /* Read the 5 INT regs in one transaction */
336 ret
= pcf50633_read_block(pcf
, PCF50633_REG_INT1
,
337 ARRAY_SIZE(pcf_int
), pcf_int
);
338 if (ret
!= ARRAY_SIZE(pcf_int
)) {
339 dev_err(pcf
->dev
, "Error reading INT registers\n");
342 * If this doesn't ACK the interrupt to the chip, we'll be
343 * called once again as we're level triggered.
348 /* We immediately read the usb and adapter status. We thus make sure
349 * only of USBINS/USBREM IRQ handlers are called */
350 if (pcf_int
[0] & (PCF50633_INT1_USBINS
| PCF50633_INT1_USBREM
)) {
351 chgstat
= pcf50633_reg_read(pcf
, PCF50633_REG_MBCS2
);
352 if (chgstat
& (0x3 << 4))
353 pcf_int
[0] &= ~(1 << PCF50633_INT1_USBREM
);
355 pcf_int
[0] &= ~(1 << PCF50633_INT1_USBINS
);
358 /* Make sure only one of ADPINS or ADPREM is set */
359 if (pcf_int
[0] & (PCF50633_INT1_ADPINS
| PCF50633_INT1_ADPREM
)) {
360 chgstat
= pcf50633_reg_read(pcf
, PCF50633_REG_MBCS2
);
361 if (chgstat
& (0x3 << 4))
362 pcf_int
[0] &= ~(1 << PCF50633_INT1_ADPREM
);
364 pcf_int
[0] &= ~(1 << PCF50633_INT1_ADPINS
);
367 dev_dbg(pcf
->dev
, "INT1=0x%02x INT2=0x%02x INT3=0x%02x "
368 "INT4=0x%02x INT5=0x%02x\n", pcf_int
[0],
369 pcf_int
[1], pcf_int
[2], pcf_int
[3], pcf_int
[4]);
371 /* Some revisions of the chip don't have a 8s standby mode on
372 * ONKEY1S press. We try to manually do it in such cases. */
373 if ((pcf_int
[0] & PCF50633_INT1_SECOND
) && pcf
->onkey1s_held
) {
374 dev_info(pcf
->dev
, "ONKEY1S held for %d secs\n",
376 if (pcf
->onkey1s_held
++ == PCF50633_ONKEY1S_TIMEOUT
)
377 if (pcf
->pdata
->force_shutdown
)
378 pcf
->pdata
->force_shutdown(pcf
);
381 if (pcf_int
[2] & PCF50633_INT3_ONKEY1S
) {
382 dev_info(pcf
->dev
, "ONKEY1S held\n");
383 pcf
->onkey1s_held
= 1 ;
385 /* Unmask IRQ_SECOND */
386 pcf50633_reg_clear_bits(pcf
, PCF50633_REG_INT1M
,
387 PCF50633_INT1_SECOND
);
389 /* Unmask IRQ_ONKEYR */
390 pcf50633_reg_clear_bits(pcf
, PCF50633_REG_INT2M
,
391 PCF50633_INT2_ONKEYR
);
394 if ((pcf_int
[1] & PCF50633_INT2_ONKEYR
) && pcf
->onkey1s_held
) {
395 pcf
->onkey1s_held
= 0;
397 /* Mask SECOND and ONKEYR interrupts */
398 if (pcf
->mask_regs
[0] & PCF50633_INT1_SECOND
)
399 pcf50633_reg_set_bit_mask(pcf
,
401 PCF50633_INT1_SECOND
,
402 PCF50633_INT1_SECOND
);
404 if (pcf
->mask_regs
[1] & PCF50633_INT2_ONKEYR
)
405 pcf50633_reg_set_bit_mask(pcf
,
407 PCF50633_INT2_ONKEYR
,
408 PCF50633_INT2_ONKEYR
);
411 /* Have we just resumed ? */
412 if (pcf
->is_suspended
) {
413 pcf
->is_suspended
= 0;
415 /* Set the resume reason filtering out non resumers */
416 for (i
= 0; i
< ARRAY_SIZE(pcf_int
); i
++)
417 pcf
->resume_reason
[i
] = pcf_int
[i
] &
418 pcf
->pdata
->resumers
[i
];
420 /* Make sure we don't pass on any ONKEY events to
422 pcf_int
[1] &= ~(PCF50633_INT2_ONKEYR
| PCF50633_INT2_ONKEYF
);
425 for (i
= 0; i
< ARRAY_SIZE(pcf_int
); i
++) {
426 /* Unset masked interrupts */
427 pcf_int
[i
] &= ~pcf
->mask_regs
[i
];
429 for (j
= 0; j
< 8 ; j
++)
430 if (pcf_int
[i
] & (1 << j
))
431 pcf50633_irq_call_handler(pcf
, (i
* 8) + j
);
435 put_device(pcf
->dev
);
436 enable_irq(pcf
->irq
);
439 static irqreturn_t
pcf50633_irq(int irq
, void *data
)
441 struct pcf50633
*pcf
= data
;
443 dev_dbg(pcf
->dev
, "pcf50633_irq\n");
445 get_device(pcf
->dev
);
446 disable_irq_nosync(pcf
->irq
);
447 schedule_work(&pcf
->irq_work
);
453 pcf50633_client_dev_register(struct pcf50633
*pcf
, const char *name
,
454 struct platform_device
**pdev
)
456 struct pcf50633_subdev_pdata
*subdev_pdata
;
459 *pdev
= platform_device_alloc(name
, -1);
461 dev_err(pcf
->dev
, "Falied to allocate %s\n", name
);
465 subdev_pdata
= kmalloc(sizeof(*subdev_pdata
), GFP_KERNEL
);
467 dev_err(pcf
->dev
, "Error allocating subdev pdata\n");
468 platform_device_put(*pdev
);
471 subdev_pdata
->pcf
= pcf
;
472 platform_device_add_data(*pdev
, subdev_pdata
, sizeof(*subdev_pdata
));
474 (*pdev
)->dev
.parent
= pcf
->dev
;
476 ret
= platform_device_add(*pdev
);
478 dev_err(pcf
->dev
, "Failed to register %s: %d\n", name
, ret
);
479 platform_device_put(*pdev
);
485 static int pcf50633_suspend(struct device
*dev
, pm_message_t state
)
487 struct pcf50633
*pcf
;
491 pcf
= dev_get_drvdata(dev
);
493 /* Make sure our interrupt handlers are not called
495 disable_irq(pcf
->irq
);
497 /* Make sure that any running IRQ worker has quit */
498 cancel_work_sync(&pcf
->irq_work
);
501 ret
= pcf50633_read_block(pcf
, PCF50633_REG_INT1M
,
502 ARRAY_SIZE(pcf
->suspend_irq_masks
),
503 pcf
->suspend_irq_masks
);
505 dev_err(pcf
->dev
, "error saving irq masks\n");
509 /* Write wakeup irq masks */
510 for (i
= 0; i
< ARRAY_SIZE(res
); i
++)
511 res
[i
] = ~pcf
->pdata
->resumers
[i
];
513 ret
= pcf50633_write_block(pcf
, PCF50633_REG_INT1M
,
514 ARRAY_SIZE(res
), &res
[0]);
516 dev_err(pcf
->dev
, "error writing wakeup irq masks\n");
520 pcf
->is_suspended
= 1;
526 static int pcf50633_resume(struct device
*dev
)
528 struct pcf50633
*pcf
;
531 pcf
= dev_get_drvdata(dev
);
533 /* Write the saved mask registers */
534 ret
= pcf50633_write_block(pcf
, PCF50633_REG_INT1M
,
535 ARRAY_SIZE(pcf
->suspend_irq_masks
),
536 pcf
->suspend_irq_masks
);
538 dev_err(pcf
->dev
, "Error restoring saved suspend masks\n");
540 /* Restore regulators' state */
543 get_device(pcf
->dev
);
546 * Clear any pending interrupts and set resume reason if any.
547 * This will leave with enable_irq()
549 pcf50633_irq_worker(&pcf
->irq_work
);
554 #define pcf50633_suspend NULL
555 #define pcf50633_resume NULL
558 static int __devinit
pcf50633_probe(struct i2c_client
*client
,
559 const struct i2c_device_id
*ids
)
561 struct pcf50633
*pcf
;
562 struct pcf50633_platform_data
*pdata
= client
->dev
.platform_data
;
564 int version
, variant
;
566 pcf
= kzalloc(sizeof(*pcf
), GFP_KERNEL
);
572 mutex_init(&pcf
->lock
);
574 i2c_set_clientdata(client
, pcf
);
575 pcf
->dev
= &client
->dev
;
576 pcf
->i2c_client
= client
;
577 pcf
->irq
= client
->irq
;
579 INIT_WORK(&pcf
->irq_work
, pcf50633_irq_worker
);
581 version
= pcf50633_reg_read(pcf
, 0);
582 variant
= pcf50633_reg_read(pcf
, 1);
583 if (version
< 0 || variant
< 0) {
584 dev_err(pcf
->dev
, "Unable to probe pcf50633\n");
589 dev_info(pcf
->dev
, "Probed device version %d variant %d\n",
592 /* Enable all interrupts except RTC SECOND */
593 pcf
->mask_regs
[0] = 0x80;
594 pcf50633_reg_write(pcf
, PCF50633_REG_INT1M
, pcf
->mask_regs
[0]);
595 pcf50633_reg_write(pcf
, PCF50633_REG_INT2M
, 0x00);
596 pcf50633_reg_write(pcf
, PCF50633_REG_INT3M
, 0x00);
597 pcf50633_reg_write(pcf
, PCF50633_REG_INT4M
, 0x00);
598 pcf50633_reg_write(pcf
, PCF50633_REG_INT5M
, 0x00);
600 /* Create sub devices */
601 pcf50633_client_dev_register(pcf
, "pcf50633-input",
603 pcf50633_client_dev_register(pcf
, "pcf50633-rtc",
605 pcf50633_client_dev_register(pcf
, "pcf50633-mbc",
607 pcf50633_client_dev_register(pcf
, "pcf50633-adc",
610 for (i
= 0; i
< PCF50633_NUM_REGULATORS
; i
++) {
611 struct platform_device
*pdev
;
613 pdev
= platform_device_alloc("pcf50633-regltr", i
);
615 dev_err(pcf
->dev
, "Cannot create regulator\n");
619 pdev
->dev
.parent
= pcf
->dev
;
620 pdev
->dev
.platform_data
= &pdata
->reg_init_data
[i
];
621 dev_set_drvdata(&pdev
->dev
, pcf
);
622 pcf
->regulator_pdev
[i
] = pdev
;
624 platform_device_add(pdev
);
628 ret
= request_irq(client
->irq
, pcf50633_irq
,
629 IRQF_TRIGGER_LOW
, "pcf50633", pcf
);
632 dev_err(pcf
->dev
, "Failed to request IRQ %d\n", ret
);
636 dev_err(pcf
->dev
, "No IRQ configured\n");
640 if (enable_irq_wake(client
->irq
) < 0)
641 dev_err(pcf
->dev
, "IRQ %u cannot be enabled as wake-up source"
642 "in this hardware revision", client
->irq
);
644 ret
= sysfs_create_group(&client
->dev
.kobj
, &pcf_attr_group
);
646 dev_err(pcf
->dev
, "error creating sysfs entries\n");
648 if (pdata
->probe_done
)
649 pdata
->probe_done(pcf
);
658 static int __devexit
pcf50633_remove(struct i2c_client
*client
)
660 struct pcf50633
*pcf
= i2c_get_clientdata(client
);
663 free_irq(pcf
->irq
, pcf
);
665 platform_device_unregister(pcf
->input_pdev
);
666 platform_device_unregister(pcf
->rtc_pdev
);
667 platform_device_unregister(pcf
->mbc_pdev
);
668 platform_device_unregister(pcf
->adc_pdev
);
670 for (i
= 0; i
< PCF50633_NUM_REGULATORS
; i
++)
671 platform_device_unregister(pcf
->regulator_pdev
[i
]);
678 static struct i2c_device_id pcf50633_id_table
[] = {
683 static struct i2c_driver pcf50633_driver
= {
686 .suspend
= pcf50633_suspend
,
687 .resume
= pcf50633_resume
,
689 .id_table
= pcf50633_id_table
,
690 .probe
= pcf50633_probe
,
691 .remove
= __devexit_p(pcf50633_remove
),
694 static int __init
pcf50633_init(void)
696 return i2c_add_driver(&pcf50633_driver
);
699 static void __exit
pcf50633_exit(void)
701 i2c_del_driver(&pcf50633_driver
);
704 MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
705 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
706 MODULE_LICENSE("GPL");
708 subsys_initcall(pcf50633_init
);
709 module_exit(pcf50633_exit
);