10 #include <linux/kernel.h>
11 #include <linux/slab.h>
17 #include <linux/module.h>
31 #define AB8500_IT_SOURCE1_REG 0x00
32 #define AB8500_IT_SOURCE2_REG 0x01
33 #define AB8500_IT_SOURCE3_REG 0x02
34 #define AB8500_IT_SOURCE4_REG 0x03
35 #define AB8500_IT_SOURCE5_REG 0x04
36 #define AB8500_IT_SOURCE6_REG 0x05
37 #define AB8500_IT_SOURCE7_REG 0x06
38 #define AB8500_IT_SOURCE8_REG 0x07
39 #define AB9540_IT_SOURCE13_REG 0x0C
40 #define AB8500_IT_SOURCE19_REG 0x12
41 #define AB8500_IT_SOURCE20_REG 0x13
42 #define AB8500_IT_SOURCE21_REG 0x14
43 #define AB8500_IT_SOURCE22_REG 0x15
44 #define AB8500_IT_SOURCE23_REG 0x16
45 #define AB8500_IT_SOURCE24_REG 0x17
50 #define AB8500_IT_LATCH1_REG 0x20
51 #define AB8500_IT_LATCH2_REG 0x21
52 #define AB8500_IT_LATCH3_REG 0x22
53 #define AB8500_IT_LATCH4_REG 0x23
54 #define AB8500_IT_LATCH5_REG 0x24
55 #define AB8500_IT_LATCH6_REG 0x25
56 #define AB8500_IT_LATCH7_REG 0x26
57 #define AB8500_IT_LATCH8_REG 0x27
58 #define AB8500_IT_LATCH9_REG 0x28
59 #define AB8500_IT_LATCH10_REG 0x29
60 #define AB8500_IT_LATCH12_REG 0x2B
61 #define AB9540_IT_LATCH13_REG 0x2C
62 #define AB8500_IT_LATCH19_REG 0x32
63 #define AB8500_IT_LATCH20_REG 0x33
64 #define AB8500_IT_LATCH21_REG 0x34
65 #define AB8500_IT_LATCH22_REG 0x35
66 #define AB8500_IT_LATCH23_REG 0x36
67 #define AB8500_IT_LATCH24_REG 0x37
73 #define AB8500_IT_MASK1_REG 0x40
74 #define AB8500_IT_MASK2_REG 0x41
75 #define AB8500_IT_MASK3_REG 0x42
76 #define AB8500_IT_MASK4_REG 0x43
77 #define AB8500_IT_MASK5_REG 0x44
78 #define AB8500_IT_MASK6_REG 0x45
79 #define AB8500_IT_MASK7_REG 0x46
80 #define AB8500_IT_MASK8_REG 0x47
81 #define AB8500_IT_MASK9_REG 0x48
82 #define AB8500_IT_MASK10_REG 0x49
83 #define AB8500_IT_MASK11_REG 0x4A
84 #define AB8500_IT_MASK12_REG 0x4B
85 #define AB8500_IT_MASK13_REG 0x4C
86 #define AB8500_IT_MASK14_REG 0x4D
87 #define AB8500_IT_MASK15_REG 0x4E
88 #define AB8500_IT_MASK16_REG 0x4F
89 #define AB8500_IT_MASK17_REG 0x50
90 #define AB8500_IT_MASK18_REG 0x51
91 #define AB8500_IT_MASK19_REG 0x52
92 #define AB8500_IT_MASK20_REG 0x53
93 #define AB8500_IT_MASK21_REG 0x54
94 #define AB8500_IT_MASK22_REG 0x55
95 #define AB8500_IT_MASK23_REG 0x56
96 #define AB8500_IT_MASK24_REG 0x57
101 #define AB8500_IT_LATCHHIER1_REG 0x60
102 #define AB8500_IT_LATCHHIER2_REG 0x61
103 #define AB8500_IT_LATCHHIER3_REG 0x62
105 #define AB8500_IT_LATCHHIER_NUM 3
107 #define AB8500_REV_REG 0x80
108 #define AB8500_IC_NAME_REG 0x82
109 #define AB8500_SWITCH_OFF_STATUS 0x00
111 #define AB8500_TURN_ON_STATUS 0x00
116 #define AB9540_MODEM_CTRL2_REG 0x23
117 #define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT BIT(2)
129 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
134 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24,
137 static const char ab8500_version_str[][7] = {
150 dev_err(ab8500->
dev,
"prcmu i2c error %d\n", ret);
154 static int ab8500_prcmu_write_masked(
struct ab8500 *ab8500,
u16 addr,
u8 mask,
162 dev_err(ab8500->
dev,
"prcmu i2c error %d\n", ret);
166 static int ab8500_prcmu_read(
struct ab8500 *ab8500,
u16 addr)
173 dev_err(ab8500->
dev,
"prcmu i2c error %d\n", ret);
179 static int ab8500_get_chip_id(
struct device *
dev)
181 struct ab8500 *ab8500;
189 static int set_register_interruptible(
struct ab8500 *ab8500,
u8 bank,
199 dev_vdbg(ab8500->
dev,
"wr: addr %#x <= %#x\n", addr, data);
203 ret = ab8500->
write(ab8500, addr, data);
205 dev_err(ab8500->
dev,
"failed to write reg %#x: %d\n",
212 static int ab8500_set_register(
struct device *dev,
u8 bank,
219 ret = set_register_interruptible(ab8500, bank, reg, value);
224 static int get_register_interruptible(
struct ab8500 *ab8500,
u8 bank,
234 ret = ab8500->
read(ab8500, addr);
236 dev_err(ab8500->
dev,
"failed to read reg %#x: %d\n",
242 dev_vdbg(ab8500->
dev,
"rd: addr %#x => data %#x\n", addr, ret);
247 static int ab8500_get_register(
struct device *dev,
u8 bank,
254 ret = get_register_interruptible(ab8500, bank, reg, value);
259 static int mask_and_set_register_interruptible(
struct ab8500 *ab8500,
u8 bank,
272 ret = ab8500->
read(ab8500, addr);
274 dev_err(ab8500->
dev,
"failed to read reg %#x: %d\n",
280 data = (~bitmask &
data) | (bitmask & bitvalues);
282 ret = ab8500->
write(ab8500, addr, data);
284 dev_err(ab8500->
dev,
"failed to write reg %#x: %d\n",
287 dev_vdbg(ab8500->
dev,
"mask: addr %#x => data %#x\n", addr,
291 ret = ab8500->
write_masked(ab8500, addr, bitmask, bitvalues);
293 dev_err(ab8500->
dev,
"failed to modify reg %#x: %d\n", addr,
300 static int ab8500_mask_and_set_register(
struct device *dev,
301 u8 bank,
u8 reg,
u8 bitmask,
u8 bitvalues)
307 ret= mask_and_set_register_interruptible(ab8500, bank, reg,
314 .get_chip_id = ab8500_get_chip_id,
315 .get_register = ab8500_get_register,
316 .set_register = ab8500_set_register,
317 .get_register_page =
NULL,
318 .set_register_page =
NULL,
319 .mask_and_set_register = ab8500_mask_and_set_register,
320 .event_registers_startup_state_get =
NULL,
321 .startup_irq_enabled =
NULL,
324 static void ab8500_irq_lock(
struct irq_data *data)
326 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
332 static void ab8500_irq_sync_unlock(
struct irq_data *data)
334 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
337 for (i = 0; i < ab8500->
mask_size; i++) {
350 is_ab8500_1p1_or_earlier(ab8500))
362 static void ab8500_irq_mask(
struct irq_data *data)
364 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
366 int index = offset / 8;
367 int mask = 1 << (offset % 8);
372 static void ab8500_irq_unmask(
struct irq_data *data)
374 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
376 int index = offset / 8;
377 int mask = 1 << (offset % 8);
382 static struct irq_chip ab8500_irq_chip = {
384 .irq_bus_lock = ab8500_irq_lock,
385 .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
386 .irq_mask = ab8500_irq_mask,
387 .irq_disable = ab8500_irq_mask,
388 .irq_unmask = ab8500_irq_unmask,
391 static int ab8500_handle_hierarchical_line(
struct ab8500 *ab8500,
392 int latch_offset,
u8 latch_val)
394 int int_bit =
__ffs(latch_val);
398 int_bit =
__ffs(latch_val);
405 dev_err(ab8500->
dev,
"Register offset 0x%2x not declared\n",
410 line = (i << 3) + int_bit;
411 latch_val &= ~(1 << int_bit);
419 static int ab8500_handle_hierarchical_latch(
struct ab8500 *ab8500,
420 int hier_offset,
u8 hier_val)
423 u8 latch_offset, latch_val;
426 latch_bit =
__ffs(hier_val);
427 latch_offset = (hier_offset << 3) + latch_bit;
433 status = get_register_interruptible(ab8500,
437 if (status < 0 || latch_val == 0)
440 status = ab8500_handle_hierarchical_line(ab8500,
441 latch_offset, latch_val);
445 hier_val &= ~(1 << latch_bit);
451 static irqreturn_t ab8500_hierarchical_irq(
int irq,
void *dev)
453 struct ab8500 *ab8500 =
dev;
465 if (status < 0 || hier_val == 0)
468 status = ab8500_handle_hierarchical_latch(ab8500, i, hier_val);
483 static int ab8500_irq_get_virq(
struct ab8500 *ab8500,
int irq)
493 struct ab8500 *ab8500 =
dev;
500 for (i = 0; i < ab8500->
mask_size; i++) {
509 if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
514 if (status < 0 || value == 0)
519 int line = i * 8 +
bit;
520 int virq = ab8500_irq_get_virq(ab8500, line);
523 value &= ~(1 <<
bit);
531 static int ab8500_irq_map(
struct irq_domain *
d,
unsigned int virq,
540 irq_set_chip_and_handler(virq, &ab8500_irq_chip,
542 irq_set_nested_thread(virq, 1);
546 irq_set_noprobe(virq);
553 .map = ab8500_irq_map,
557 static int ab8500_irq_init(
struct ab8500 *ab8500,
struct device_node *np)
561 if (is_ab9540(ab8500))
563 else if (is_ab8505(ab8500))
571 0, &ab8500_irq_ops, ab8500);
575 np, num_irqs, &ab8500_irq_ops, ab8500);
579 dev_err(ab8500->
dev,
"Failed to create irqdomain\n");
613 .name =
"GPIO_INT14",
619 .name =
"GPIO_INT15",
628 .name =
"HW_CONV_END",
634 .name =
"SW_CONV_END",
673 .name =
"ACC_DETECT_1DB_F",
679 .name =
"ACC_DETECT_1DB_R",
685 .name =
"ACC_DETECT_21DB_F",
691 .name =
"ACC_DETECT_21DB_R",
697 .name =
"ACC_DETECT_22DB_F",
703 .name =
"ACC_DETECT_22DB_R",
712 .name =
"MAIN_CH_UNPLUG_DET",
718 .name =
"MAIN_CHARGE_PLUG_DET",
724 .name =
"VBUS_DET_R",
730 .name =
"VBUS_DET_F",
736 .name =
"USB_LINK_STATUS",
748 .name =
"USB_CH_TH_PROT_R",
754 .name =
"USB_CH_TH_PROT_F",
760 .name =
"MAIN_EXT_CH_NOT_OK",
766 .name =
"MAIN_CH_TH_PROT_R",
772 .name =
"MAIN_CH_TH_PROT_F",
778 .name =
"USB_CHARGER_NOT_OKR",
793 .name =
"BAT_CTRL_INDB",
805 .name =
"BTEMP_HIGH",
811 .name =
"BTEMP_LOW_MEDIUM",
817 .name =
"BTEMP_MEDIUM_HIGH",
826 .name =
"NCONV_ACCU",
850 .name =
"CC_INT_CALIB",
865 #ifdef CONFIG_DEBUG_FS
884 .name =
"ID_WAKEUP_R",
890 .name =
"ID_WAKEUP_F",
896 .name =
"VBUS_DET_F",
902 .name =
"VBUS_DET_R",
908 .name =
"USB_LINK_STATUS",
914 .name =
"USB_ADP_PROBE_PLUG",
920 .name =
"USB_ADP_PROBE_UNPLUG",
929 .name =
"KeyDeglitch",
962 .name =
"AB8500_TEMP_WARM",
970 #ifdef CONFIG_DEBUG_FS
972 .name =
"ab8500-debug",
973 .of_compatible =
"stericsson,ab8500-debug",
974 .num_resources =
ARRAY_SIZE(ab8500_debug_resources),
975 .resources = ab8500_debug_resources,
979 .
name =
"ab8500-sysctrl",
980 .of_compatible =
"stericsson,ab8500-sysctrl",
983 .name =
"ab8500-regulator",
984 .of_compatible =
"stericsson,ab8500-regulator",
987 .name =
"ab8500-gpadc",
988 .of_compatible =
"stericsson,ab8500-gpadc",
989 .num_resources =
ARRAY_SIZE(ab8500_gpadc_resources),
990 .resources = ab8500_gpadc_resources,
993 .
name =
"ab8500-rtc",
994 .of_compatible =
"stericsson,ab8500-rtc",
995 .num_resources =
ARRAY_SIZE(ab8500_rtc_resources),
996 .resources = ab8500_rtc_resources,
999 .
name =
"ab8500-acc-det",
1000 .of_compatible =
"stericsson,ab8500-acc-det",
1001 .num_resources =
ARRAY_SIZE(ab8500_av_acc_detect_resources),
1002 .resources = ab8500_av_acc_detect_resources,
1005 .
name =
"ab8500-poweron-key",
1006 .of_compatible =
"stericsson,ab8500-poweron-key",
1007 .num_resources =
ARRAY_SIZE(ab8500_poweronkey_db_resources),
1008 .resources = ab8500_poweronkey_db_resources,
1011 .
name =
"ab8500-pwm",
1012 .of_compatible =
"stericsson,ab8500-pwm",
1016 .name =
"ab8500-pwm",
1017 .of_compatible =
"stericsson,ab8500-pwm",
1021 .name =
"ab8500-pwm",
1022 .of_compatible =
"stericsson,ab8500-pwm",
1026 .name =
"ab8500-leds",
1027 .of_compatible =
"stericsson,ab8500-leds",
1030 .name =
"ab8500-denc",
1031 .of_compatible =
"stericsson,ab8500-denc",
1034 .name =
"ab8500-temp",
1035 .of_compatible =
"stericsson,ab8500-temp",
1036 .num_resources =
ARRAY_SIZE(ab8500_temp_resources),
1037 .resources = ab8500_temp_resources,
1043 .name =
"ab8500-charger",
1044 .num_resources =
ARRAY_SIZE(ab8500_charger_resources),
1045 .resources = ab8500_charger_resources,
1048 .
name =
"ab8500-btemp",
1049 .num_resources =
ARRAY_SIZE(ab8500_btemp_resources),
1050 .resources = ab8500_btemp_resources,
1053 .
name =
"ab8500-fg",
1054 .num_resources =
ARRAY_SIZE(ab8500_fg_resources),
1055 .resources = ab8500_fg_resources,
1058 .
name =
"ab8500-chargalg",
1059 .num_resources =
ARRAY_SIZE(ab8500_chargalg_resources),
1060 .resources = ab8500_chargalg_resources,
1066 .name =
"ab8500-gpio",
1067 .of_compatible =
"stericsson,ab8500-gpio",
1068 .num_resources =
ARRAY_SIZE(ab8500_gpio_resources),
1069 .resources = ab8500_gpio_resources,
1072 .
name =
"ab8500-usb",
1073 .of_compatible =
"stericsson,ab8500-usb",
1074 .num_resources =
ARRAY_SIZE(ab8500_usb_resources),
1075 .resources = ab8500_usb_resources,
1078 .
name =
"ab8500-codec",
1079 .of_compatible =
"stericsson,ab8500-codec",
1085 .name =
"ab8500-gpio",
1086 .num_resources =
ARRAY_SIZE(ab9540_gpio_resources),
1087 .resources = ab9540_gpio_resources,
1090 .
name =
"ab9540-usb",
1091 .num_resources =
ARRAY_SIZE(ab8500_usb_resources),
1092 .resources = ab8500_usb_resources,
1095 .
name =
"ab9540-codec",
1103 .num_resources =
ARRAY_SIZE(ab8505_iddet_resources),
1104 .resources = ab8505_iddet_resources,
1111 struct ab8500 *ab8500;
1133 struct ab8500 *ab8500;
1136 ret = get_register_interruptible(ab8500,
AB8500_RTC,
1140 return sprintf(buf,
"%#x\n", value);
1159 struct ab8500 *ab8500;
1166 return sprintf(buf,
"%#x\n", value);
1172 struct ab8500 *ab8500;
1190 struct ab8500 *ab8500;
1209 err = mask_and_set_register_interruptible(ab8500,
1214 "Failed to set DBBRSTN %c, err %#x\n",
1226 show_ab9540_dbbrstn, store_ab9540_dbbrstn);
1228 static struct attribute *ab8500_sysfs_entries[] = {
1229 &dev_attr_chip_id.attr,
1230 &dev_attr_switch_off_status.attr,
1231 &dev_attr_turn_on_status.attr,
1235 static struct attribute *ab9540_sysfs_entries[] = {
1236 &dev_attr_chip_id.attr,
1237 &dev_attr_switch_off_status.attr,
1238 &dev_attr_turn_on_status.attr,
1239 &dev_attr_dbbrstn.attr,
1244 .attrs = ab8500_sysfs_entries,
1248 .attrs = ab9540_sysfs_entries,
1253 static char *switch_off_status[] = {
1254 "Swoff bit programming",
1255 "Thermal protection activation",
1256 "Vbat lower then BattOk falling threshold",
1258 "Non presence of 32kHz clock",
1259 "Battery level lower than power on reset threshold",
1260 "Power on key 1 pressed longer than 10 seconds",
1261 "DB8500 thermal shutdown"};
1266 struct ab8500 *ab8500;
1272 ab8500 = kzalloc(
sizeof *ab8500,
GFP_KERNEL);
1279 ab8500->
dev = &pdev->
dev;
1284 goto out_free_ab8500;
1289 ab8500->
read = ab8500_prcmu_read;
1290 ab8500->
write = ab8500_prcmu_write;
1297 platform_set_drvdata(pdev, ab8500);
1300 version = platid->driver_data;
1305 ret = get_register_interruptible(ab8500,
AB8500_MISC,
1308 goto out_free_ab8500;
1313 ret = get_register_interruptible(ab8500,
AB8500_MISC,
1316 goto out_free_ab8500;
1320 dev_info(ab8500->
dev,
"detected chip, %s rev. %1x.%1x\n",
1321 ab8500_version_str[ab8500->
version],
1326 if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
1353 ret = get_register_interruptible(ab8500,
AB8500_RTC,
1357 dev_info(ab8500->
dev,
"switch off cause(s) (%#x): ", value);
1360 for (i = 0; i <
ARRAY_SIZE(switch_off_status); i++) {
1363 switch_off_status[i]);
1372 if (plat && plat->
init)
1376 for (i = 0; i < ab8500->
mask_size; i++) {
1382 is_ab8500_1p1_or_earlier(ab8500))
1394 goto out_freeoldmask;
1399 ret = ab8500_irq_init(ab8500, np);
1401 goto out_freeoldmask;
1405 if (is_ab9540(ab8500)) {
1407 ab8500_hierarchical_irq,
1417 goto out_freeoldmask;
1426 if (is_ab9540(ab8500))
1437 if (is_ab9540(ab8500) || is_ab8505(ab8500))
1450 dev_err(ab8500->
dev,
"error adding bm devices\n");
1453 if (is_ab9540(ab8500))
1455 &ab9540_attr_group);
1458 &ab8500_attr_group);
1460 dev_err(ab8500->
dev,
"error creating sysfs entries\n");
1478 struct ab8500 *ab8500 = platform_get_drvdata(pdev);
1480 if (is_ab9540(ab8500))
1505 .name =
"ab8500-core",
1508 .probe = ab8500_probe,
1510 .id_table = ab8500_id,
1513 static int __init ab8500_core_init(
void)
1518 static void __exit ab8500_core_exit(
void)
1525 MODULE_AUTHOR(
"Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");