2 * tps65910-core.c -- Multifunction core driver for TPS65910x chips
4 * Copyright (C) 2010 Mistral solutions Pvt Ltd <www.mistralsolutions.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/mutex.h>
25 #include <linux/platform_device.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
29 #include <linux/regulator/machine.h>
31 #include <linux/i2c.h>
32 #include <linux/i2c/tps65910.h>
33 #include <mach/board.h>
34 #include <mach/gpio.h>
35 #include <linux/irq.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
40 #define DBG(x...) printk(KERN_INFO x)
44 #define TPS65910_SPEED 400 * 1000
47 #define DRIVER_NAME "tps65910"
49 #if defined(CONFIG_GPIO_TPS65910)
50 #define tps65910_has_gpio() true
52 #define tps65910_has_gpio() false
55 #if defined(CONFIG_REGULATOR_TPS65910)
56 #define tps65910_has_regulator() true
58 #define tps65910_has_regulator() false
61 #if defined(CONFIG_RTC_DRV_TPS65910)
62 #define tps65910_has_rtc() true
64 #define tps65910_has_rtc() false
67 #define TPS65910_GENERAL 0
68 #define TPS65910_SMARTREFLEX 1
71 struct tps65910_platform_data *gtps65910_platform = NULL;
73 enum tps65910x_model {
74 TPS65910, /* TI processors OMAP3 family */
75 TPS659101, /* Samsung - S5PV210, S5PC1xx */
76 TPS659102, /* Samsung - S3C64xx */
77 TPS659103, /* Reserved */
78 TPS659104, /* Reserved */
79 TPS659105, /* TI processors - DM643x, DM644x */
80 TPS659106, /* Reserved */
81 TPS659107, /* Reserved */
82 TPS659108, /* Reserved */
83 TPS659109, /* Freescale - i.MX51 */
88 static struct work_struct core_work;
89 static struct mutex work_lock;
91 /* Structure for each TPS65910 Slave */
92 struct tps65910_client {
93 struct i2c_client *client;
95 /* max numb of i2c_msg required for read = 2 */
96 struct i2c_msg xfer_msg[2];
97 /* To lock access to xfer_msg */
98 struct mutex xfer_lock;
100 static struct tps65910_client tps65910_modules[TPS65910_NUM_SLAVES];
102 /* bbch = Back-up battery charger control register */
103 int tps65910_enable_bbch(u8 voltage)
108 if (voltage == TPS65910_BBSEL_3P0 || voltage == TPS65910_BBSEL_2P52 ||
109 voltage == TPS65910_BBSEL_3P15 ||
110 voltage == TPS65910_BBSEL_VBAT) {
111 val = (voltage | TPS65910_BBCHEN);
112 err = tps65910_i2c_write_u8(TPS65910_I2C_ID0, val,
115 printk(KERN_ERR "Unable write TPS65910_REG_BBCH reg\n");
119 printk(KERN_ERR"Invalid argumnet for %s \n", __func__);
125 EXPORT_SYMBOL(tps65910_enable_bbch);
127 int tps65910_disable_bbch(void)
132 err = tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_BBCH);
135 val &= ~TPS65910_BBCHEN;
137 err = tps65910_i2c_write_u8(TPS65910_I2C_ID0, val,
140 printk(KERN_ERR "Unable write TPS65910_REG_BBCH \
145 printk(KERN_ERR "Unable to read TPS65910_REG_BBCH reg\n");
150 EXPORT_SYMBOL(tps65910_disable_bbch);
152 int tps65910_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
154 struct tps65910_client *tps65910;
158 case TPS65910_I2C_ID0:
159 tps65910 = &tps65910_modules[0];
160 tps65910->address = TPS65910_I2C_ID0;
162 case TPS65910_I2C_ID1:
163 tps65910 = &tps65910_modules[1];
164 tps65910->address = TPS65910_I2C_ID1;
167 printk(KERN_ERR "Invalid Slave address for TPS65910\n");
171 ret = i2c_master_reg8_recv(tps65910->client, reg, (char *)value, 1, TPS65910_SPEED);
172 DBG("%s: ret=%d, slave_addr=0x%x, reg=0x%x, value=0x%x\n", __FUNCTION__, (ret > 0 ? 0: -EINVAL), mod_no, reg, *value);
174 return (ret > 0 ? 0: -EINVAL);
176 EXPORT_SYMBOL(tps65910_i2c_read_u8);
178 int tps65910_i2c_write_u8(u8 slave_addr, u8 value, u8 reg)
180 struct tps65910_client *tps65910;
183 switch (slave_addr) {
184 case TPS65910_I2C_ID0:
185 tps65910 = &tps65910_modules[0];
186 tps65910->address = TPS65910_I2C_ID0;
188 case TPS65910_I2C_ID1:
189 tps65910 = &tps65910_modules[1];
190 tps65910->address = TPS65910_I2C_ID1;
193 printk(KERN_ERR "Invalid Slave address for TPS65910\n");
197 ret = i2c_master_reg8_send(tps65910->client, reg, (char *)&value, 1, TPS65910_SPEED);
198 DBG("%s: ret=%d, slave_addr=0x%x, reg=0x%x, value=0x%x\n", __FUNCTION__, ret, slave_addr, reg, value);
205 EXPORT_SYMBOL(tps65910_i2c_write_u8);
208 int tps65910_enable_irq(int irq)
214 tps65910_i2c_read_u8(TPS65910_I2C_ID0,
215 &mask, TPS65910_REG_INT_MSK2);
217 return tps65910_i2c_write_u8(TPS65910_I2C_ID0,
218 mask, TPS65910_REG_INT_MSK2);
220 tps65910_i2c_read_u8(TPS65910_I2C_ID0,
221 &mask, TPS65910_REG_INT_MSK);
223 return tps65910_i2c_write_u8(TPS65910_I2C_ID0,
224 mask, TPS65910_REG_INT_MSK);
227 EXPORT_SYMBOL(tps65910_enable_irq);
229 int tps65910_disable_irq(int irq)
235 tps65910_i2c_read_u8(TPS65910_I2C_ID0,
236 &mask, TPS65910_REG_INT_MSK2);
238 return tps65910_i2c_write_u8(TPS65910_I2C_ID0,
239 mask, TPS65910_REG_INT_MSK2);
241 tps65910_i2c_read_u8(TPS65910_I2C_ID0,
242 &mask, TPS65910_REG_INT_MSK);
244 return tps65910_i2c_write_u8(TPS65910_I2C_ID0,
245 mask, TPS65910_REG_INT_MSK);
248 EXPORT_SYMBOL(tps65910_disable_irq);
250 int tps65910_add_irq_work(int irq,
251 void (*handler)(void *data))
254 gtps65910_platform->handlers[irq] = handler;
255 ret = tps65910_enable_irq(irq);
259 EXPORT_SYMBOL(tps65910_add_irq_work);
261 int tps65910_remove_irq_work(int irq)
264 ret = tps65910_disable_irq(irq);
265 gtps65910_platform->handlers[irq] = NULL;
268 EXPORT_SYMBOL(tps65910_remove_irq_work);
270 static void tps65910_core_work(struct work_struct *work)
272 /* Read the status register and take action */
279 void (*handler)(void *data) = NULL;
281 DBG("Enter::%s %d\n",__FUNCTION__,__LINE__);
282 mutex_lock(&work_lock);
284 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &status2,
285 TPS65910_REG_INT_STS2);
286 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &mask2,
287 TPS65910_REG_INT_MSK2);
289 isr = (status2 << 8);
290 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &status,
291 TPS65910_REG_INT_STS);
292 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &mask,
293 TPS65910_REG_INT_MSK);
302 handler = gtps65910_platform->handlers[irq];
304 handler(gtps65910_platform);
307 enable_irq(gtps65910_platform->irq_num);
308 mutex_unlock(&work_lock);
312 static irqreturn_t tps65910_isr(int irq, void *data)
314 disable_irq_nosync(irq);
315 (void) schedule_work(&core_work);
320 static struct device *add_numbered_child(unsigned chip, const char *name,
321 int num, void *pdata, unsigned pdata_len, bool can_wakeup, int irq)
324 struct platform_device *pdev;
325 struct tps65910_client *tps65910 = &tps65910_modules[chip];
328 pdev = platform_device_alloc(name, num);
330 dev_dbg(&tps65910->client->dev, "can't alloc dev\n");
334 device_init_wakeup(&pdev->dev, can_wakeup);
335 pdev->dev.parent = &tps65910->client->dev;
338 status = platform_device_add_data(pdev, pdata, pdata_len);
340 dev_dbg(&pdev->dev, "can't add platform_data\n");
344 status = platform_device_add(pdev);
348 platform_device_put(pdev);
349 dev_err(&tps65910->client->dev, "can't add %s dev\n", name);
350 return ERR_PTR(status);
356 static inline struct device *add_child(unsigned chip, const char *name,
357 void *pdata, unsigned pdata_len,
358 bool can_wakeup, int irq)
360 return add_numbered_child(chip, name, -1, pdata, pdata_len,
365 struct device *add_regulator_linked(int num, struct regulator_init_data *pdata,
366 struct regulator_consumer_supply *consumers,
367 unsigned num_consumers)
369 /* regulator framework demands init_data */
374 pdata->consumer_supplies = consumers;
375 pdata->num_consumer_supplies = num_consumers;
377 return add_numbered_child(TPS65910_GENERAL, "tps65910_regulator", num,
378 pdata, sizeof(*pdata), false, TPS65910_HOST_IRQ);
381 static struct device *
382 add_regulator(int num, struct regulator_init_data *pdata)
384 return add_regulator_linked(num, pdata, NULL, 0);
388 add_children(struct tps65910_platform_data *pdata, unsigned long features)
391 struct device *child;
393 struct platform_device *pdev = NULL;
395 if (tps65910_has_gpio() && (pdata->gpio != NULL)) {
397 pdev = platform_device_alloc("tps65910_gpio", -1);
402 pdev->dev.parent = &tps65910_modules[0].client->dev;
403 device_init_wakeup(&pdev->dev, 0);
405 status = platform_device_add_data(pdev, pdata,
409 "can't add platform_data\n");
414 if (tps65910_has_rtc()) {
415 child = add_child(TPS65910_GENERAL, "tps65910_rtc",
416 NULL, 0, true, pdata->irq_num);
418 return PTR_ERR(child);
421 if (tps65910_has_regulator()) {
423 child = add_regulator(TPS65910_VIO, pdata->vio);
425 return PTR_ERR(child);
427 child = add_regulator(TPS65910_VDD1, pdata->vdd1);
429 return PTR_ERR(child);
431 child = add_regulator(TPS65910_VDD2, pdata->vdd2);
433 return PTR_ERR(child);
435 child = add_regulator(TPS65910_VDD3, pdata->vdd3);
437 return PTR_ERR(child);
439 child = add_regulator(TPS65910_VDIG1, pdata->vdig1);
441 return PTR_ERR(child);
443 child = add_regulator(TPS65910_VDIG2, pdata->vdig2);
445 return PTR_ERR(child);
447 child = add_regulator(TPS65910_VAUX33, pdata->vaux33);
449 return PTR_ERR(child);
451 child = add_regulator(TPS65910_VMMC, pdata->vmmc);
453 return PTR_ERR(child);
455 child = add_regulator(TPS65910_VAUX1, pdata->vaux1);
457 return PTR_ERR(child);
459 child = add_regulator(TPS65910_VAUX2, pdata->vaux2);
461 return PTR_ERR(child);
463 child = add_regulator(TPS65910_VDAC, pdata->vdac);
465 return PTR_ERR(child);
467 child = add_regulator(TPS65910_VPLL, pdata->vpll);
469 return PTR_ERR(child);
478 static int tps65910_remove(struct i2c_client *client)
482 for (i = 0; i < TPS65910_NUM_SLAVES; i++) {
484 struct tps65910_client *tps65910 = &tps65910_modules[i];
486 if (tps65910->client && tps65910->client != client)
487 i2c_unregister_device(tps65910->client);
489 tps65910_modules[i].client = NULL;
496 tps65910_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
500 struct tps65910_platform_data *pdata;
502 pdata = client->dev.platform_data;
503 gtps65910_platform = pdata;
505 DBG("cwz: tps65910_i2c_probe\n");
508 dev_dbg(&client->dev, "no platform data?\n");
512 if (!i2c_check_functionality(client->adapter,I2C_FUNC_I2C)) {
513 dev_dbg(&client->dev, "can't talk I2C?\n");
518 dev_dbg(&client->dev, "driver is already in use\n");
521 for (i = 0; i < TPS65910_NUM_SLAVES; i++) {
523 struct tps65910_client *tps65910 = &tps65910_modules[i];
525 tps65910->address = client->addr;
528 tps65910->client = client;
530 tps65910->client = i2c_new_dummy(client->adapter,
533 if (!tps65910->client) {
534 dev_err(&client->dev,
535 "can't attach client %d\n", i);
540 mutex_init(&tps65910->xfer_lock);
545 if (pdata->board_tps65910_config != NULL)
546 pdata->board_tps65910_config(pdata);
548 if (pdata->irq_num) {
549 /* TPS65910 power ON interrupt(s) would have already been
550 * occurred, so immediately after request_irq the control will
551 * be transferred to tps65910_isr, if we do core_work
552 * initialization after requesting IRQ, the system crashes
553 * and does not boot; to avoid this we do core_work
554 * initialization before requesting IRQ
556 mutex_init(&work_lock);
558 if(gpio_request(client->irq, "tps65910 irq"))
560 dev_err(&client->dev, "gpio request fail\n");
561 gpio_free(client->irq);
565 pdata->irq_num = gpio_to_irq(client->irq);
566 gpio_pull_updown(client->irq,GPIOPullUp);
568 status = request_irq(pdata->irq_num, tps65910_isr,
569 IRQF_TRIGGER_FALLING, client->dev.driver->name, pdata);
571 pr_err("tps65910: could not claim irq%d: %d\n",
572 pdata->irq_num, status);
575 enable_irq_wake(pdata->irq_num);
576 INIT_WORK(&core_work, tps65910_core_work);
579 status = add_children(pdata, 0x00);
587 tps65910_remove(client);
593 static int tps65910_i2c_remove(struct i2c_client *client)
597 for (i = 0; i < TPS65910_NUM_SLAVES; i++) {
599 struct tps65910_client *tps65910 = &tps65910_modules[i];
601 if (tps65910->client && tps65910->client != client)
602 i2c_unregister_device(tps65910->client);
604 tps65910_modules[i].client = NULL;
610 /* chip-specific feature flags, for i2c_device_id.driver_data */
611 static const struct i2c_device_id tps65910_i2c_ids[] = {
612 { "tps65910", TPS65910 },
613 { "tps659101", TPS659101 },
614 { "tps659102", TPS659102 },
615 { "tps659103", TPS659103 },
616 { "tps659104", TPS659104 },
617 { "tps659105", TPS659105 },
618 { "tps659106", TPS659106 },
619 { "tps659107", TPS659107 },
620 { "tps659108", TPS659108 },
621 { "tps659109", TPS659109 },
622 {/* end of list */ },
624 MODULE_DEVICE_TABLE(i2c, tps65910_i2c_ids);
626 /* One Client Driver ,3 Clients - Regulator, RTC , GPIO */
627 static struct i2c_driver tps65910_i2c_driver = {
630 .owner = THIS_MODULE,
632 .id_table = tps65910_i2c_ids,
633 .probe = tps65910_i2c_probe,
634 .remove = __devexit_p(tps65910_i2c_remove),
637 static int __init tps65910_init(void)
641 res = i2c_add_driver(&tps65910_i2c_driver);
643 pr_err(DRIVER_NAME ": driver registration failed\n");
649 subsys_initcall_sync(tps65910_init);
651 static void __exit tps65910_exit(void)
653 i2c_del_driver(&tps65910_i2c_driver);
655 module_exit(tps65910_exit);
658 #ifdef CONFIG_PROC_FS
659 #include <linux/proc_fs.h>
660 #include <linux/seq_file.h>
662 static int proc_tps65910_show(struct seq_file *s, void *v)
666 seq_printf(s, "\n\nTPS65910 Registers is:\n");
668 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_REF);
669 seq_printf(s, "REF_REG=0x%x, Value=0x%x\n", TPS65910_REG_REF, val);
670 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VRTC);
671 seq_printf(s, "VRTC_REG=0x%x, Value=0x%x\n", TPS65910_REG_VRTC, val);
673 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDD1);
674 seq_printf(s, "VDD1_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDD1, val);
675 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDD1_OP);
676 seq_printf(s, "VDD1_OP_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDD1_OP, val);
678 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDD2);
679 seq_printf(s, "VDD2_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDD2, val);
680 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDD2_OP);
681 seq_printf(s, "VDD2_OP_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDD2_OP, val);
683 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VIO);
684 seq_printf(s, "VIO_REG=0x%x, Value=0x%x\n", TPS65910_REG_VIO, val);
686 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDIG1);
687 seq_printf(s, "VDIG1_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDIG1, val);
688 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDIG2);
689 seq_printf(s, "VDIG2_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDIG2, val);
690 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VAUX1);
691 seq_printf(s, "VAUX1_REG=0x%x, Value=0x%x\n", TPS65910_REG_VAUX1, val);
692 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VAUX2);
693 seq_printf(s, "VAUX2_REG=0x%x, Value=0x%x\n", TPS65910_REG_VAUX2, val);
694 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VAUX33);
695 seq_printf(s, "VAUX33_REG=0x%x, Value=0x%x\n", TPS65910_REG_VAUX33, val);
696 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VMMC);
697 seq_printf(s, "VMMC_REG=0x%x, Value=0x%x\n", TPS65910_REG_VMMC, val);
698 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VPLL);
699 seq_printf(s, "VPLL_REG=0x%x, Value=0x%x\n", TPS65910_REG_VPLL, val);
700 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_VDAC);
701 seq_printf(s, "VDAC_REG=0x%x, Value=0x%x\n", TPS65910_REG_VDAC, val);
703 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_DEVCTRL);
704 seq_printf(s, "DEVCTRL_REG=0x%x, Value=0x%x\n", TPS65910_REG_DEVCTRL, val);
705 tps65910_i2c_read_u8(TPS65910_I2C_ID0, &val, TPS65910_REG_DEVCTRL2);
706 seq_printf(s, "DEVCTRL2_REG=0x%x, Value=0x%x\n", TPS65910_REG_DEVCTRL2, val);
708 #if 0 // cwz 1 test vcore
710 struct regulator *vldo;
712 vldo = regulator_get(NULL, "vcore");
717 seq_printf(s, "Set VCORE.\n");
718 regulator_set_voltage(vldo,1100000,1100000);
720 uV = regulator_get_voltage(vldo);
721 seq_printf(s, "Get VCORE=%d(uV).\n", uV);
728 static int proc_tps65910_open(struct inode *inode, struct file *file)
730 return single_open(file, proc_tps65910_show, NULL);
733 static const struct file_operations proc_tps65910_fops = {
734 .open = proc_tps65910_open,
737 .release = single_release,
740 static int __init proc_tps65910_init(void)
742 proc_create("tps65910", 0, NULL, &proc_tps65910_fops);
745 late_initcall(proc_tps65910_init);
746 #endif /* CONFIG_PROC_FS */
748 MODULE_AUTHOR("cwz <cwz@rock-chips.com>");
749 MODULE_DESCRIPTION("I2C Core interface for TPS65910");
750 MODULE_LICENSE("GPL");