xref: /linux/drivers/mfd/da9052-core.c (revision e0c21530fa91f119bfca19640a67380c6b14f12a)
184c99db8SAshish Jangam /*
284c99db8SAshish Jangam  * Device access for Dialog DA9052 PMICs.
384c99db8SAshish Jangam  *
484c99db8SAshish Jangam  * Copyright(c) 2011 Dialog Semiconductor Ltd.
584c99db8SAshish Jangam  *
684c99db8SAshish Jangam  * Author: David Dajun Chen <dchen@diasemi.com>
784c99db8SAshish Jangam  *
884c99db8SAshish Jangam  *  This program is free software; you can redistribute  it and/or modify it
984c99db8SAshish Jangam  *  under  the terms of  the GNU General  Public License as published by the
1084c99db8SAshish Jangam  *  Free Software Foundation;  either version 2 of the  License, or (at your
1184c99db8SAshish Jangam  *  option) any later version.
1284c99db8SAshish Jangam  */
1384c99db8SAshish Jangam 
1484c99db8SAshish Jangam #include <linux/device.h>
1584c99db8SAshish Jangam #include <linux/delay.h>
1684c99db8SAshish Jangam #include <linux/input.h>
1784c99db8SAshish Jangam #include <linux/interrupt.h>
1884c99db8SAshish Jangam #include <linux/mfd/core.h>
1984c99db8SAshish Jangam #include <linux/slab.h>
2084c99db8SAshish Jangam #include <linux/module.h>
2184c99db8SAshish Jangam 
2284c99db8SAshish Jangam #include <linux/mfd/da9052/da9052.h>
2384c99db8SAshish Jangam #include <linux/mfd/da9052/pdata.h>
2484c99db8SAshish Jangam #include <linux/mfd/da9052/reg.h>
2584c99db8SAshish Jangam 
2684c99db8SAshish Jangam static bool da9052_reg_readable(struct device *dev, unsigned int reg)
2784c99db8SAshish Jangam {
2884c99db8SAshish Jangam 	switch (reg) {
2984c99db8SAshish Jangam 	case DA9052_PAGE0_CON_REG:
3084c99db8SAshish Jangam 	case DA9052_STATUS_A_REG:
3184c99db8SAshish Jangam 	case DA9052_STATUS_B_REG:
3284c99db8SAshish Jangam 	case DA9052_STATUS_C_REG:
3384c99db8SAshish Jangam 	case DA9052_STATUS_D_REG:
3484c99db8SAshish Jangam 	case DA9052_EVENT_A_REG:
3584c99db8SAshish Jangam 	case DA9052_EVENT_B_REG:
3684c99db8SAshish Jangam 	case DA9052_EVENT_C_REG:
3784c99db8SAshish Jangam 	case DA9052_EVENT_D_REG:
3884c99db8SAshish Jangam 	case DA9052_FAULTLOG_REG:
3984c99db8SAshish Jangam 	case DA9052_IRQ_MASK_A_REG:
4084c99db8SAshish Jangam 	case DA9052_IRQ_MASK_B_REG:
4184c99db8SAshish Jangam 	case DA9052_IRQ_MASK_C_REG:
4284c99db8SAshish Jangam 	case DA9052_IRQ_MASK_D_REG:
4384c99db8SAshish Jangam 	case DA9052_CONTROL_A_REG:
4484c99db8SAshish Jangam 	case DA9052_CONTROL_B_REG:
4584c99db8SAshish Jangam 	case DA9052_CONTROL_C_REG:
4684c99db8SAshish Jangam 	case DA9052_CONTROL_D_REG:
4784c99db8SAshish Jangam 	case DA9052_PDDIS_REG:
4884c99db8SAshish Jangam 	case DA9052_INTERFACE_REG:
4984c99db8SAshish Jangam 	case DA9052_RESET_REG:
5084c99db8SAshish Jangam 	case DA9052_GPIO_0_1_REG:
5184c99db8SAshish Jangam 	case DA9052_GPIO_2_3_REG:
5284c99db8SAshish Jangam 	case DA9052_GPIO_4_5_REG:
5384c99db8SAshish Jangam 	case DA9052_GPIO_6_7_REG:
5484c99db8SAshish Jangam 	case DA9052_GPIO_14_15_REG:
5584c99db8SAshish Jangam 	case DA9052_ID_0_1_REG:
5684c99db8SAshish Jangam 	case DA9052_ID_2_3_REG:
5784c99db8SAshish Jangam 	case DA9052_ID_4_5_REG:
5884c99db8SAshish Jangam 	case DA9052_ID_6_7_REG:
5984c99db8SAshish Jangam 	case DA9052_ID_8_9_REG:
6084c99db8SAshish Jangam 	case DA9052_ID_10_11_REG:
6184c99db8SAshish Jangam 	case DA9052_ID_12_13_REG:
6284c99db8SAshish Jangam 	case DA9052_ID_14_15_REG:
6384c99db8SAshish Jangam 	case DA9052_ID_16_17_REG:
6484c99db8SAshish Jangam 	case DA9052_ID_18_19_REG:
6584c99db8SAshish Jangam 	case DA9052_ID_20_21_REG:
6684c99db8SAshish Jangam 	case DA9052_SEQ_STATUS_REG:
6784c99db8SAshish Jangam 	case DA9052_SEQ_A_REG:
6884c99db8SAshish Jangam 	case DA9052_SEQ_B_REG:
6984c99db8SAshish Jangam 	case DA9052_SEQ_TIMER_REG:
7084c99db8SAshish Jangam 	case DA9052_BUCKA_REG:
7184c99db8SAshish Jangam 	case DA9052_BUCKB_REG:
7284c99db8SAshish Jangam 	case DA9052_BUCKCORE_REG:
7384c99db8SAshish Jangam 	case DA9052_BUCKPRO_REG:
7484c99db8SAshish Jangam 	case DA9052_BUCKMEM_REG:
7584c99db8SAshish Jangam 	case DA9052_BUCKPERI_REG:
7684c99db8SAshish Jangam 	case DA9052_LDO1_REG:
7784c99db8SAshish Jangam 	case DA9052_LDO2_REG:
7884c99db8SAshish Jangam 	case DA9052_LDO3_REG:
7984c99db8SAshish Jangam 	case DA9052_LDO4_REG:
8084c99db8SAshish Jangam 	case DA9052_LDO5_REG:
8184c99db8SAshish Jangam 	case DA9052_LDO6_REG:
8284c99db8SAshish Jangam 	case DA9052_LDO7_REG:
8384c99db8SAshish Jangam 	case DA9052_LDO8_REG:
8484c99db8SAshish Jangam 	case DA9052_LDO9_REG:
8584c99db8SAshish Jangam 	case DA9052_LDO10_REG:
8684c99db8SAshish Jangam 	case DA9052_SUPPLY_REG:
8784c99db8SAshish Jangam 	case DA9052_PULLDOWN_REG:
8884c99db8SAshish Jangam 	case DA9052_CHGBUCK_REG:
8984c99db8SAshish Jangam 	case DA9052_WAITCONT_REG:
9084c99db8SAshish Jangam 	case DA9052_ISET_REG:
9184c99db8SAshish Jangam 	case DA9052_BATCHG_REG:
9284c99db8SAshish Jangam 	case DA9052_CHG_CONT_REG:
9384c99db8SAshish Jangam 	case DA9052_INPUT_CONT_REG:
9484c99db8SAshish Jangam 	case DA9052_CHG_TIME_REG:
9584c99db8SAshish Jangam 	case DA9052_BBAT_CONT_REG:
9684c99db8SAshish Jangam 	case DA9052_BOOST_REG:
9784c99db8SAshish Jangam 	case DA9052_LED_CONT_REG:
9884c99db8SAshish Jangam 	case DA9052_LEDMIN123_REG:
9984c99db8SAshish Jangam 	case DA9052_LED1_CONF_REG:
10084c99db8SAshish Jangam 	case DA9052_LED2_CONF_REG:
10184c99db8SAshish Jangam 	case DA9052_LED3_CONF_REG:
10284c99db8SAshish Jangam 	case DA9052_LED1CONT_REG:
10384c99db8SAshish Jangam 	case DA9052_LED2CONT_REG:
10484c99db8SAshish Jangam 	case DA9052_LED3CONT_REG:
10584c99db8SAshish Jangam 	case DA9052_LED_CONT_4_REG:
10684c99db8SAshish Jangam 	case DA9052_LED_CONT_5_REG:
10784c99db8SAshish Jangam 	case DA9052_ADC_MAN_REG:
10884c99db8SAshish Jangam 	case DA9052_ADC_CONT_REG:
10984c99db8SAshish Jangam 	case DA9052_ADC_RES_L_REG:
11084c99db8SAshish Jangam 	case DA9052_ADC_RES_H_REG:
11184c99db8SAshish Jangam 	case DA9052_VDD_RES_REG:
11284c99db8SAshish Jangam 	case DA9052_VDD_MON_REG:
11384c99db8SAshish Jangam 	case DA9052_ICHG_AV_REG:
11484c99db8SAshish Jangam 	case DA9052_ICHG_THD_REG:
11584c99db8SAshish Jangam 	case DA9052_ICHG_END_REG:
11684c99db8SAshish Jangam 	case DA9052_TBAT_RES_REG:
11784c99db8SAshish Jangam 	case DA9052_TBAT_HIGHP_REG:
11884c99db8SAshish Jangam 	case DA9052_TBAT_HIGHN_REG:
11984c99db8SAshish Jangam 	case DA9052_TBAT_LOW_REG:
12084c99db8SAshish Jangam 	case DA9052_T_OFFSET_REG:
12184c99db8SAshish Jangam 	case DA9052_ADCIN4_RES_REG:
12284c99db8SAshish Jangam 	case DA9052_AUTO4_HIGH_REG:
12384c99db8SAshish Jangam 	case DA9052_AUTO4_LOW_REG:
12484c99db8SAshish Jangam 	case DA9052_ADCIN5_RES_REG:
12584c99db8SAshish Jangam 	case DA9052_AUTO5_HIGH_REG:
12684c99db8SAshish Jangam 	case DA9052_AUTO5_LOW_REG:
12784c99db8SAshish Jangam 	case DA9052_ADCIN6_RES_REG:
12884c99db8SAshish Jangam 	case DA9052_AUTO6_HIGH_REG:
12984c99db8SAshish Jangam 	case DA9052_AUTO6_LOW_REG:
13084c99db8SAshish Jangam 	case DA9052_TJUNC_RES_REG:
13184c99db8SAshish Jangam 	case DA9052_TSI_CONT_A_REG:
13284c99db8SAshish Jangam 	case DA9052_TSI_CONT_B_REG:
13384c99db8SAshish Jangam 	case DA9052_TSI_X_MSB_REG:
13484c99db8SAshish Jangam 	case DA9052_TSI_Y_MSB_REG:
13584c99db8SAshish Jangam 	case DA9052_TSI_LSB_REG:
13684c99db8SAshish Jangam 	case DA9052_TSI_Z_MSB_REG:
13784c99db8SAshish Jangam 	case DA9052_COUNT_S_REG:
13884c99db8SAshish Jangam 	case DA9052_COUNT_MI_REG:
13984c99db8SAshish Jangam 	case DA9052_COUNT_H_REG:
14084c99db8SAshish Jangam 	case DA9052_COUNT_D_REG:
14184c99db8SAshish Jangam 	case DA9052_COUNT_MO_REG:
14284c99db8SAshish Jangam 	case DA9052_COUNT_Y_REG:
14384c99db8SAshish Jangam 	case DA9052_ALARM_MI_REG:
14484c99db8SAshish Jangam 	case DA9052_ALARM_H_REG:
14584c99db8SAshish Jangam 	case DA9052_ALARM_D_REG:
14684c99db8SAshish Jangam 	case DA9052_ALARM_MO_REG:
14784c99db8SAshish Jangam 	case DA9052_ALARM_Y_REG:
14884c99db8SAshish Jangam 	case DA9052_SECOND_A_REG:
14984c99db8SAshish Jangam 	case DA9052_SECOND_B_REG:
15084c99db8SAshish Jangam 	case DA9052_SECOND_C_REG:
15184c99db8SAshish Jangam 	case DA9052_SECOND_D_REG:
15284c99db8SAshish Jangam 	case DA9052_PAGE1_CON_REG:
15384c99db8SAshish Jangam 		return true;
15484c99db8SAshish Jangam 	default:
15584c99db8SAshish Jangam 		return false;
15684c99db8SAshish Jangam 	}
15784c99db8SAshish Jangam }
15884c99db8SAshish Jangam 
15984c99db8SAshish Jangam static bool da9052_reg_writeable(struct device *dev, unsigned int reg)
16084c99db8SAshish Jangam {
16184c99db8SAshish Jangam 	switch (reg) {
16284c99db8SAshish Jangam 	case DA9052_PAGE0_CON_REG:
1630a92815dSAshish Jangam 	case DA9052_EVENT_A_REG:
1640a92815dSAshish Jangam 	case DA9052_EVENT_B_REG:
1650a92815dSAshish Jangam 	case DA9052_EVENT_C_REG:
1660a92815dSAshish Jangam 	case DA9052_EVENT_D_REG:
16784c99db8SAshish Jangam 	case DA9052_IRQ_MASK_A_REG:
16884c99db8SAshish Jangam 	case DA9052_IRQ_MASK_B_REG:
16984c99db8SAshish Jangam 	case DA9052_IRQ_MASK_C_REG:
17084c99db8SAshish Jangam 	case DA9052_IRQ_MASK_D_REG:
17184c99db8SAshish Jangam 	case DA9052_CONTROL_A_REG:
17284c99db8SAshish Jangam 	case DA9052_CONTROL_B_REG:
17384c99db8SAshish Jangam 	case DA9052_CONTROL_C_REG:
17484c99db8SAshish Jangam 	case DA9052_CONTROL_D_REG:
17584c99db8SAshish Jangam 	case DA9052_PDDIS_REG:
17684c99db8SAshish Jangam 	case DA9052_RESET_REG:
17784c99db8SAshish Jangam 	case DA9052_GPIO_0_1_REG:
17884c99db8SAshish Jangam 	case DA9052_GPIO_2_3_REG:
17984c99db8SAshish Jangam 	case DA9052_GPIO_4_5_REG:
18084c99db8SAshish Jangam 	case DA9052_GPIO_6_7_REG:
18184c99db8SAshish Jangam 	case DA9052_GPIO_14_15_REG:
18284c99db8SAshish Jangam 	case DA9052_ID_0_1_REG:
18384c99db8SAshish Jangam 	case DA9052_ID_2_3_REG:
18484c99db8SAshish Jangam 	case DA9052_ID_4_5_REG:
18584c99db8SAshish Jangam 	case DA9052_ID_6_7_REG:
18684c99db8SAshish Jangam 	case DA9052_ID_8_9_REG:
18784c99db8SAshish Jangam 	case DA9052_ID_10_11_REG:
18884c99db8SAshish Jangam 	case DA9052_ID_12_13_REG:
18984c99db8SAshish Jangam 	case DA9052_ID_14_15_REG:
19084c99db8SAshish Jangam 	case DA9052_ID_16_17_REG:
19184c99db8SAshish Jangam 	case DA9052_ID_18_19_REG:
19284c99db8SAshish Jangam 	case DA9052_ID_20_21_REG:
19384c99db8SAshish Jangam 	case DA9052_SEQ_STATUS_REG:
19484c99db8SAshish Jangam 	case DA9052_SEQ_A_REG:
19584c99db8SAshish Jangam 	case DA9052_SEQ_B_REG:
19684c99db8SAshish Jangam 	case DA9052_SEQ_TIMER_REG:
19784c99db8SAshish Jangam 	case DA9052_BUCKA_REG:
19884c99db8SAshish Jangam 	case DA9052_BUCKB_REG:
19984c99db8SAshish Jangam 	case DA9052_BUCKCORE_REG:
20084c99db8SAshish Jangam 	case DA9052_BUCKPRO_REG:
20184c99db8SAshish Jangam 	case DA9052_BUCKMEM_REG:
20284c99db8SAshish Jangam 	case DA9052_BUCKPERI_REG:
20384c99db8SAshish Jangam 	case DA9052_LDO1_REG:
20484c99db8SAshish Jangam 	case DA9052_LDO2_REG:
20584c99db8SAshish Jangam 	case DA9052_LDO3_REG:
20684c99db8SAshish Jangam 	case DA9052_LDO4_REG:
20784c99db8SAshish Jangam 	case DA9052_LDO5_REG:
20884c99db8SAshish Jangam 	case DA9052_LDO6_REG:
20984c99db8SAshish Jangam 	case DA9052_LDO7_REG:
21084c99db8SAshish Jangam 	case DA9052_LDO8_REG:
21184c99db8SAshish Jangam 	case DA9052_LDO9_REG:
21284c99db8SAshish Jangam 	case DA9052_LDO10_REG:
21384c99db8SAshish Jangam 	case DA9052_SUPPLY_REG:
21484c99db8SAshish Jangam 	case DA9052_PULLDOWN_REG:
21584c99db8SAshish Jangam 	case DA9052_CHGBUCK_REG:
21684c99db8SAshish Jangam 	case DA9052_WAITCONT_REG:
21784c99db8SAshish Jangam 	case DA9052_ISET_REG:
21884c99db8SAshish Jangam 	case DA9052_BATCHG_REG:
21984c99db8SAshish Jangam 	case DA9052_CHG_CONT_REG:
22084c99db8SAshish Jangam 	case DA9052_INPUT_CONT_REG:
22184c99db8SAshish Jangam 	case DA9052_BBAT_CONT_REG:
22284c99db8SAshish Jangam 	case DA9052_BOOST_REG:
22384c99db8SAshish Jangam 	case DA9052_LED_CONT_REG:
22484c99db8SAshish Jangam 	case DA9052_LEDMIN123_REG:
22584c99db8SAshish Jangam 	case DA9052_LED1_CONF_REG:
22684c99db8SAshish Jangam 	case DA9052_LED2_CONF_REG:
22784c99db8SAshish Jangam 	case DA9052_LED3_CONF_REG:
22884c99db8SAshish Jangam 	case DA9052_LED1CONT_REG:
22984c99db8SAshish Jangam 	case DA9052_LED2CONT_REG:
23084c99db8SAshish Jangam 	case DA9052_LED3CONT_REG:
23184c99db8SAshish Jangam 	case DA9052_LED_CONT_4_REG:
23284c99db8SAshish Jangam 	case DA9052_LED_CONT_5_REG:
23384c99db8SAshish Jangam 	case DA9052_ADC_MAN_REG:
23484c99db8SAshish Jangam 	case DA9052_ADC_CONT_REG:
23584c99db8SAshish Jangam 	case DA9052_ADC_RES_L_REG:
23684c99db8SAshish Jangam 	case DA9052_ADC_RES_H_REG:
23784c99db8SAshish Jangam 	case DA9052_VDD_RES_REG:
23884c99db8SAshish Jangam 	case DA9052_VDD_MON_REG:
23984c99db8SAshish Jangam 	case DA9052_ICHG_THD_REG:
24084c99db8SAshish Jangam 	case DA9052_ICHG_END_REG:
24184c99db8SAshish Jangam 	case DA9052_TBAT_HIGHP_REG:
24284c99db8SAshish Jangam 	case DA9052_TBAT_HIGHN_REG:
24384c99db8SAshish Jangam 	case DA9052_TBAT_LOW_REG:
24484c99db8SAshish Jangam 	case DA9052_T_OFFSET_REG:
24584c99db8SAshish Jangam 	case DA9052_AUTO4_HIGH_REG:
24684c99db8SAshish Jangam 	case DA9052_AUTO4_LOW_REG:
24784c99db8SAshish Jangam 	case DA9052_AUTO5_HIGH_REG:
24884c99db8SAshish Jangam 	case DA9052_AUTO5_LOW_REG:
24984c99db8SAshish Jangam 	case DA9052_AUTO6_HIGH_REG:
25084c99db8SAshish Jangam 	case DA9052_AUTO6_LOW_REG:
25184c99db8SAshish Jangam 	case DA9052_TSI_CONT_A_REG:
25284c99db8SAshish Jangam 	case DA9052_TSI_CONT_B_REG:
25384c99db8SAshish Jangam 	case DA9052_COUNT_S_REG:
25484c99db8SAshish Jangam 	case DA9052_COUNT_MI_REG:
25584c99db8SAshish Jangam 	case DA9052_COUNT_H_REG:
25684c99db8SAshish Jangam 	case DA9052_COUNT_D_REG:
25784c99db8SAshish Jangam 	case DA9052_COUNT_MO_REG:
25884c99db8SAshish Jangam 	case DA9052_COUNT_Y_REG:
25984c99db8SAshish Jangam 	case DA9052_ALARM_MI_REG:
26084c99db8SAshish Jangam 	case DA9052_ALARM_H_REG:
26184c99db8SAshish Jangam 	case DA9052_ALARM_D_REG:
26284c99db8SAshish Jangam 	case DA9052_ALARM_MO_REG:
26384c99db8SAshish Jangam 	case DA9052_ALARM_Y_REG:
26484c99db8SAshish Jangam 	case DA9052_PAGE1_CON_REG:
26584c99db8SAshish Jangam 		return true;
26684c99db8SAshish Jangam 	default:
26784c99db8SAshish Jangam 		return false;
26884c99db8SAshish Jangam 	}
26984c99db8SAshish Jangam }
27084c99db8SAshish Jangam 
27184c99db8SAshish Jangam static bool da9052_reg_volatile(struct device *dev, unsigned int reg)
27284c99db8SAshish Jangam {
27384c99db8SAshish Jangam 	switch (reg) {
27484c99db8SAshish Jangam 	case DA9052_STATUS_A_REG:
27584c99db8SAshish Jangam 	case DA9052_STATUS_B_REG:
27684c99db8SAshish Jangam 	case DA9052_STATUS_C_REG:
27784c99db8SAshish Jangam 	case DA9052_STATUS_D_REG:
27884c99db8SAshish Jangam 	case DA9052_EVENT_A_REG:
27984c99db8SAshish Jangam 	case DA9052_EVENT_B_REG:
28084c99db8SAshish Jangam 	case DA9052_EVENT_C_REG:
28184c99db8SAshish Jangam 	case DA9052_EVENT_D_REG:
282bcb5a7d3Santhony.olech.opensource@diasemi.com 	case DA9052_CONTROL_B_REG:
283bcb5a7d3Santhony.olech.opensource@diasemi.com 	case DA9052_CONTROL_D_REG:
284bcb5a7d3Santhony.olech.opensource@diasemi.com 	case DA9052_SUPPLY_REG:
28584c99db8SAshish Jangam 	case DA9052_FAULTLOG_REG:
28684c99db8SAshish Jangam 	case DA9052_CHG_TIME_REG:
28784c99db8SAshish Jangam 	case DA9052_ADC_RES_L_REG:
28884c99db8SAshish Jangam 	case DA9052_ADC_RES_H_REG:
28984c99db8SAshish Jangam 	case DA9052_VDD_RES_REG:
29084c99db8SAshish Jangam 	case DA9052_ICHG_AV_REG:
29184c99db8SAshish Jangam 	case DA9052_TBAT_RES_REG:
29284c99db8SAshish Jangam 	case DA9052_ADCIN4_RES_REG:
29384c99db8SAshish Jangam 	case DA9052_ADCIN5_RES_REG:
29484c99db8SAshish Jangam 	case DA9052_ADCIN6_RES_REG:
29584c99db8SAshish Jangam 	case DA9052_TJUNC_RES_REG:
29684c99db8SAshish Jangam 	case DA9052_TSI_X_MSB_REG:
29784c99db8SAshish Jangam 	case DA9052_TSI_Y_MSB_REG:
29884c99db8SAshish Jangam 	case DA9052_TSI_LSB_REG:
29984c99db8SAshish Jangam 	case DA9052_TSI_Z_MSB_REG:
30084c99db8SAshish Jangam 	case DA9052_COUNT_S_REG:
30184c99db8SAshish Jangam 	case DA9052_COUNT_MI_REG:
30284c99db8SAshish Jangam 	case DA9052_COUNT_H_REG:
30384c99db8SAshish Jangam 	case DA9052_COUNT_D_REG:
30484c99db8SAshish Jangam 	case DA9052_COUNT_MO_REG:
30584c99db8SAshish Jangam 	case DA9052_COUNT_Y_REG:
30684c99db8SAshish Jangam 	case DA9052_ALARM_MI_REG:
30784c99db8SAshish Jangam 		return true;
30884c99db8SAshish Jangam 	default:
30984c99db8SAshish Jangam 		return false;
31084c99db8SAshish Jangam 	}
31184c99db8SAshish Jangam }
31284c99db8SAshish Jangam 
31316e5e204SAshish Jangam /*
31416e5e204SAshish Jangam  * TBAT look-up table is computed from the R90 reg (8 bit register)
31516e5e204SAshish Jangam  * reading as below. The battery temperature is in milliCentigrade
31616e5e204SAshish Jangam  * TBAT = (1/(t1+1/298) - 273) * 1000 mC
31716e5e204SAshish Jangam  * where t1 = (1/B)* ln(( ADCval * 2.5)/(R25*ITBAT*255))
31816e5e204SAshish Jangam  * Default values are R25 = 10e3, B = 3380, ITBAT = 50e-6
31916e5e204SAshish Jangam  * Example:
32016e5e204SAshish Jangam  * R25=10E3, B=3380, ITBAT=50e-6, ADCVAL=62d calculates
32116e5e204SAshish Jangam  * TBAT = 20015 mili degrees Centrigrade
32216e5e204SAshish Jangam  *
32316e5e204SAshish Jangam */
32416e5e204SAshish Jangam static const int32_t tbat_lookup[255] = {
32516e5e204SAshish Jangam 	183258, 144221, 124334, 111336, 101826, 94397, 88343, 83257,
32616e5e204SAshish Jangam 	78889, 75071, 71688, 68656, 65914, 63414, 61120, 59001,
32716e5e204SAshish Jangam 	570366, 55204, 53490, 51881, 50364, 48931, 47574, 46285,
32816e5e204SAshish Jangam 	45059, 43889, 42772, 41703, 40678, 39694, 38748, 37838,
32916e5e204SAshish Jangam 	36961, 36115, 35297, 34507, 33743, 33002, 32284, 31588,
33016e5e204SAshish Jangam 	30911, 30254, 29615, 28994, 28389, 27799, 27225, 26664,
33116e5e204SAshish Jangam 	26117, 25584, 25062, 24553, 24054, 23567, 23091, 22624,
33216e5e204SAshish Jangam 	22167, 21719, 21281, 20851, 20429, 20015, 19610, 19211,
33316e5e204SAshish Jangam 	18820, 18436, 18058, 17688, 17323, 16965, 16612, 16266,
33416e5e204SAshish Jangam 	15925, 15589, 15259, 14933, 14613, 14298, 13987, 13681,
33516e5e204SAshish Jangam 	13379, 13082, 12788, 12499, 12214, 11933, 11655, 11382,
33616e5e204SAshish Jangam 	11112, 10845, 10582, 10322, 10066, 9812, 9562, 9315,
33716e5e204SAshish Jangam 	9071, 8830, 8591, 8356, 8123, 7893, 7665, 7440,
33816e5e204SAshish Jangam 	7218, 6998, 6780, 6565, 6352, 6141, 5933, 5726,
33916e5e204SAshish Jangam 	5522, 5320, 5120, 4922, 4726, 4532, 4340, 4149,
34016e5e204SAshish Jangam 	3961, 3774, 3589, 3406, 3225, 3045, 2867, 2690,
34116e5e204SAshish Jangam 	2516, 2342, 2170, 2000, 1831, 1664, 1498, 1334,
34216e5e204SAshish Jangam 	1171, 1009, 849, 690, 532, 376, 221, 67,
34316e5e204SAshish Jangam 	-84, -236, -386, -535, -683, -830, -975, -1119,
34416e5e204SAshish Jangam 	-1263, -1405, -1546, -1686, -1825, -1964, -2101, -2237,
34516e5e204SAshish Jangam 	-2372, -2506, -2639, -2771, -2902, -3033, -3162, -3291,
34616e5e204SAshish Jangam 	-3418, -3545, -3671, -3796, -3920, -4044, -4166, -4288,
34716e5e204SAshish Jangam 	-4409, -4529, -4649, -4767, -4885, -5002, -5119, -5235,
34816e5e204SAshish Jangam 	-5349, -5464, -5577, -5690, -5802, -5913, -6024, -6134,
34916e5e204SAshish Jangam 	-6244, -6352, -6461, -6568, -6675, -6781, -6887, -6992,
35016e5e204SAshish Jangam 	-7096, -7200, -7303, -7406, -7508, -7609, -7710, -7810,
35116e5e204SAshish Jangam 	-7910, -8009, -8108, -8206, -8304, -8401, -8497, -8593,
35216e5e204SAshish Jangam 	-8689, -8784, -8878, -8972, -9066, -9159, -9251, -9343,
35316e5e204SAshish Jangam 	-9435, -9526, -9617, -9707, -9796, -9886, -9975, -10063,
35416e5e204SAshish Jangam 	-10151, -10238, -10325, -10412, -10839, -10923, -11007, -11090,
35516e5e204SAshish Jangam 	-11173, -11256, -11338, -11420, -11501, -11583, -11663, -11744,
35616e5e204SAshish Jangam 	-11823, -11903, -11982
35716e5e204SAshish Jangam };
35816e5e204SAshish Jangam 
35916e5e204SAshish Jangam static const u8 chan_mux[DA9052_ADC_VBBAT + 1] = {
36016e5e204SAshish Jangam 	[DA9052_ADC_VDDOUT]	= DA9052_ADC_MAN_MUXSEL_VDDOUT,
36116e5e204SAshish Jangam 	[DA9052_ADC_ICH]	= DA9052_ADC_MAN_MUXSEL_ICH,
36216e5e204SAshish Jangam 	[DA9052_ADC_TBAT]	= DA9052_ADC_MAN_MUXSEL_TBAT,
36316e5e204SAshish Jangam 	[DA9052_ADC_VBAT]	= DA9052_ADC_MAN_MUXSEL_VBAT,
36416e5e204SAshish Jangam 	[DA9052_ADC_IN4]	= DA9052_ADC_MAN_MUXSEL_AD4,
36516e5e204SAshish Jangam 	[DA9052_ADC_IN5]	= DA9052_ADC_MAN_MUXSEL_AD5,
36616e5e204SAshish Jangam 	[DA9052_ADC_IN6]	= DA9052_ADC_MAN_MUXSEL_AD6,
36716e5e204SAshish Jangam 	[DA9052_ADC_VBBAT]	= DA9052_ADC_MAN_MUXSEL_VBBAT
36816e5e204SAshish Jangam };
36916e5e204SAshish Jangam 
37016e5e204SAshish Jangam int da9052_adc_manual_read(struct da9052 *da9052, unsigned char channel)
37116e5e204SAshish Jangam {
37216e5e204SAshish Jangam 	int ret;
37316e5e204SAshish Jangam 	unsigned short calc_data;
37416e5e204SAshish Jangam 	unsigned short data;
37516e5e204SAshish Jangam 	unsigned char mux_sel;
37616e5e204SAshish Jangam 
37716e5e204SAshish Jangam 	if (channel > DA9052_ADC_VBBAT)
37816e5e204SAshish Jangam 		return -EINVAL;
37916e5e204SAshish Jangam 
38016e5e204SAshish Jangam 	mutex_lock(&da9052->auxadc_lock);
38116e5e204SAshish Jangam 
38216e5e204SAshish Jangam 	/* Channel gets activated on enabling the Conversion bit */
38316e5e204SAshish Jangam 	mux_sel = chan_mux[channel] | DA9052_ADC_MAN_MAN_CONV;
38416e5e204SAshish Jangam 
38516e5e204SAshish Jangam 	ret = da9052_reg_write(da9052, DA9052_ADC_MAN_REG, mux_sel);
38616e5e204SAshish Jangam 	if (ret < 0)
38716e5e204SAshish Jangam 		goto err;
38816e5e204SAshish Jangam 
38916e5e204SAshish Jangam 	/* Wait for an interrupt */
39016e5e204SAshish Jangam 	if (!wait_for_completion_timeout(&da9052->done,
39116e5e204SAshish Jangam 					 msecs_to_jiffies(500))) {
39216e5e204SAshish Jangam 		dev_err(da9052->dev,
39316e5e204SAshish Jangam 			"timeout waiting for ADC conversion interrupt\n");
39416e5e204SAshish Jangam 		ret = -ETIMEDOUT;
39516e5e204SAshish Jangam 		goto err;
39616e5e204SAshish Jangam 	}
39716e5e204SAshish Jangam 
39816e5e204SAshish Jangam 	ret = da9052_reg_read(da9052, DA9052_ADC_RES_H_REG);
39916e5e204SAshish Jangam 	if (ret < 0)
40016e5e204SAshish Jangam 		goto err;
40116e5e204SAshish Jangam 
40216e5e204SAshish Jangam 	calc_data = (unsigned short)ret;
40316e5e204SAshish Jangam 	data = calc_data << 2;
40416e5e204SAshish Jangam 
40516e5e204SAshish Jangam 	ret = da9052_reg_read(da9052, DA9052_ADC_RES_L_REG);
40616e5e204SAshish Jangam 	if (ret < 0)
40716e5e204SAshish Jangam 		goto err;
40816e5e204SAshish Jangam 
40916e5e204SAshish Jangam 	calc_data = (unsigned short)(ret & DA9052_ADC_RES_LSB);
41016e5e204SAshish Jangam 	data |= calc_data;
41116e5e204SAshish Jangam 
41216e5e204SAshish Jangam 	ret = data;
41316e5e204SAshish Jangam 
41416e5e204SAshish Jangam err:
41516e5e204SAshish Jangam 	mutex_unlock(&da9052->auxadc_lock);
41616e5e204SAshish Jangam 	return ret;
41716e5e204SAshish Jangam }
41816e5e204SAshish Jangam EXPORT_SYMBOL_GPL(da9052_adc_manual_read);
41916e5e204SAshish Jangam 
42016e5e204SAshish Jangam int da9052_adc_read_temp(struct da9052 *da9052)
42116e5e204SAshish Jangam {
42216e5e204SAshish Jangam 	int tbat;
42316e5e204SAshish Jangam 
42416e5e204SAshish Jangam 	tbat = da9052_reg_read(da9052, DA9052_TBAT_RES_REG);
42516e5e204SAshish Jangam 	if (tbat <= 0)
42616e5e204SAshish Jangam 		return tbat;
42716e5e204SAshish Jangam 
42816e5e204SAshish Jangam 	/* ARRAY_SIZE check is not needed since TBAT is a 8-bit register */
42916e5e204SAshish Jangam 	return tbat_lookup[tbat - 1];
43016e5e204SAshish Jangam }
43116e5e204SAshish Jangam EXPORT_SYMBOL_GPL(da9052_adc_read_temp);
43216e5e204SAshish Jangam 
433c8f675ffSGeert Uytterhoeven static const struct mfd_cell da9052_subdev_info[] = {
43484c99db8SAshish Jangam 	{
43584c99db8SAshish Jangam 		.name = "da9052-regulator",
436*e0c21530SJohan Hovold 		.id = 0,
437*e0c21530SJohan Hovold 	},
438*e0c21530SJohan Hovold 	{
439*e0c21530SJohan Hovold 		.name = "da9052-regulator",
44084c99db8SAshish Jangam 		.id = 1,
44184c99db8SAshish Jangam 	},
44284c99db8SAshish Jangam 	{
44384c99db8SAshish Jangam 		.name = "da9052-regulator",
44484c99db8SAshish Jangam 		.id = 2,
44584c99db8SAshish Jangam 	},
44684c99db8SAshish Jangam 	{
44784c99db8SAshish Jangam 		.name = "da9052-regulator",
44884c99db8SAshish Jangam 		.id = 3,
44984c99db8SAshish Jangam 	},
45084c99db8SAshish Jangam 	{
45184c99db8SAshish Jangam 		.name = "da9052-regulator",
45284c99db8SAshish Jangam 		.id = 4,
45384c99db8SAshish Jangam 	},
45484c99db8SAshish Jangam 	{
45584c99db8SAshish Jangam 		.name = "da9052-regulator",
45684c99db8SAshish Jangam 		.id = 5,
45784c99db8SAshish Jangam 	},
45884c99db8SAshish Jangam 	{
45984c99db8SAshish Jangam 		.name = "da9052-regulator",
46084c99db8SAshish Jangam 		.id = 6,
46184c99db8SAshish Jangam 	},
46284c99db8SAshish Jangam 	{
46384c99db8SAshish Jangam 		.name = "da9052-regulator",
46484c99db8SAshish Jangam 		.id = 7,
46584c99db8SAshish Jangam 	},
46684c99db8SAshish Jangam 	{
46784c99db8SAshish Jangam 		.name = "da9052-regulator",
46884c99db8SAshish Jangam 		.id = 8,
46984c99db8SAshish Jangam 	},
47084c99db8SAshish Jangam 	{
47184c99db8SAshish Jangam 		.name = "da9052-regulator",
47284c99db8SAshish Jangam 		.id = 9,
47384c99db8SAshish Jangam 	},
47484c99db8SAshish Jangam 	{
47584c99db8SAshish Jangam 		.name = "da9052-regulator",
47684c99db8SAshish Jangam 		.id = 10,
47784c99db8SAshish Jangam 	},
47884c99db8SAshish Jangam 	{
47984c99db8SAshish Jangam 		.name = "da9052-regulator",
48084c99db8SAshish Jangam 		.id = 11,
48184c99db8SAshish Jangam 	},
48284c99db8SAshish Jangam 	{
48384c99db8SAshish Jangam 		.name = "da9052-regulator",
48484c99db8SAshish Jangam 		.id = 12,
48584c99db8SAshish Jangam 	},
48684c99db8SAshish Jangam 	{
48784c99db8SAshish Jangam 		.name = "da9052-regulator",
48884c99db8SAshish Jangam 		.id = 13,
48984c99db8SAshish Jangam 	},
49084c99db8SAshish Jangam 	{
49184c99db8SAshish Jangam 		.name = "da9052-onkey",
49284c99db8SAshish Jangam 	},
49384c99db8SAshish Jangam 	{
49484c99db8SAshish Jangam 		.name = "da9052-rtc",
49584c99db8SAshish Jangam 	},
49684c99db8SAshish Jangam 	{
49784c99db8SAshish Jangam 		.name = "da9052-gpio",
49884c99db8SAshish Jangam 	},
49984c99db8SAshish Jangam 	{
50084c99db8SAshish Jangam 		.name = "da9052-hwmon",
50184c99db8SAshish Jangam 	},
50284c99db8SAshish Jangam 	{
50384c99db8SAshish Jangam 		.name = "da9052-leds",
50484c99db8SAshish Jangam 	},
50584c99db8SAshish Jangam 	{
50684c99db8SAshish Jangam 		.name = "da9052-wled1",
50784c99db8SAshish Jangam 	},
50884c99db8SAshish Jangam 	{
50984c99db8SAshish Jangam 		.name = "da9052-wled2",
51084c99db8SAshish Jangam 	},
51184c99db8SAshish Jangam 	{
51284c99db8SAshish Jangam 		.name = "da9052-wled3",
51384c99db8SAshish Jangam 	},
51484c99db8SAshish Jangam 	{
51584c99db8SAshish Jangam 		.name = "da9052-tsi",
51684c99db8SAshish Jangam 	},
51784c99db8SAshish Jangam 	{
51884c99db8SAshish Jangam 		.name = "da9052-bat",
51984c99db8SAshish Jangam 	},
52084c99db8SAshish Jangam 	{
52184c99db8SAshish Jangam 		.name = "da9052-watchdog",
52284c99db8SAshish Jangam 	},
52384c99db8SAshish Jangam };
52484c99db8SAshish Jangam 
525e9e9d397SAxel Lin const struct regmap_config da9052_regmap_config = {
52684c99db8SAshish Jangam 	.reg_bits = 8,
52784c99db8SAshish Jangam 	.val_bits = 8,
52884c99db8SAshish Jangam 
52984c99db8SAshish Jangam 	.cache_type = REGCACHE_RBTREE,
53084c99db8SAshish Jangam 
53184c99db8SAshish Jangam 	.max_register = DA9052_PAGE1_CON_REG,
53284c99db8SAshish Jangam 	.readable_reg = da9052_reg_readable,
53384c99db8SAshish Jangam 	.writeable_reg = da9052_reg_writeable,
53484c99db8SAshish Jangam 	.volatile_reg = da9052_reg_volatile,
53584c99db8SAshish Jangam };
53684c99db8SAshish Jangam EXPORT_SYMBOL_GPL(da9052_regmap_config);
53784c99db8SAshish Jangam 
538f791be49SBill Pemberton int da9052_device_init(struct da9052 *da9052, u8 chip_id)
53984c99db8SAshish Jangam {
540334a41ceSJingoo Han 	struct da9052_pdata *pdata = dev_get_platdata(da9052->dev);
54184c99db8SAshish Jangam 	int ret;
54284c99db8SAshish Jangam 
54316e5e204SAshish Jangam 	mutex_init(&da9052->auxadc_lock);
54416e5e204SAshish Jangam 	init_completion(&da9052->done);
54516e5e204SAshish Jangam 
54684c99db8SAshish Jangam 	if (pdata && pdata->init != NULL)
54784c99db8SAshish Jangam 		pdata->init(da9052);
54884c99db8SAshish Jangam 
54984c99db8SAshish Jangam 	da9052->chip_id = chip_id;
55084c99db8SAshish Jangam 
5518bad1abdSFabio Estevam 	ret = da9052_irq_init(da9052);
5528bad1abdSFabio Estevam 	if (ret != 0) {
5538bad1abdSFabio Estevam 		dev_err(da9052->dev, "da9052_irq_init failed: %d\n", ret);
5548bad1abdSFabio Estevam 		return ret;
555ffe20b68SFabio Estevam 	}
55616e5e204SAshish Jangam 
557b3f6c73dSFabio Estevam 	ret = mfd_add_devices(da9052->dev, PLATFORM_DEVID_AUTO,
558b3f6c73dSFabio Estevam 			      da9052_subdev_info,
5590848c94fSMark Brown 			      ARRAY_SIZE(da9052_subdev_info), NULL, 0, NULL);
560ffe20b68SFabio Estevam 	if (ret) {
561ffe20b68SFabio Estevam 		dev_err(da9052->dev, "mfd_add_devices failed: %d\n", ret);
56284c99db8SAshish Jangam 		goto err;
563ffe20b68SFabio Estevam 	}
56484c99db8SAshish Jangam 
56584c99db8SAshish Jangam 	return 0;
56684c99db8SAshish Jangam 
56784c99db8SAshish Jangam err:
5688bad1abdSFabio Estevam 	da9052_irq_exit(da9052);
5698bad1abdSFabio Estevam 
57084c99db8SAshish Jangam 	return ret;
57184c99db8SAshish Jangam }
57284c99db8SAshish Jangam 
57384c99db8SAshish Jangam void da9052_device_exit(struct da9052 *da9052)
57484c99db8SAshish Jangam {
57584c99db8SAshish Jangam 	mfd_remove_devices(da9052->dev);
5768bad1abdSFabio Estevam 	da9052_irq_exit(da9052);
57784c99db8SAshish Jangam }
57884c99db8SAshish Jangam 
57984c99db8SAshish Jangam MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
58084c99db8SAshish Jangam MODULE_DESCRIPTION("DA9052 MFD Core");
58184c99db8SAshish Jangam MODULE_LICENSE("GPL");
582