• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * License Terms: GNU General Public License v2
5  * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
6  * Author: Rabin Vincent <rabin.vincent@stericsson.com>
7  * Author: Mattias Wallin <mattias.wallin@stericsson.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/abx500.h>
20 #include <linux/mfd/abx500/ab8500.h>
21 #include <linux/regulator/ab8500.h>
22 
23 /*
24  * Interrupt register offsets
25  * Bank : 0x0E
26  */
27 #define AB8500_IT_SOURCE1_REG		0x00
28 #define AB8500_IT_SOURCE2_REG		0x01
29 #define AB8500_IT_SOURCE3_REG		0x02
30 #define AB8500_IT_SOURCE4_REG		0x03
31 #define AB8500_IT_SOURCE5_REG		0x04
32 #define AB8500_IT_SOURCE6_REG		0x05
33 #define AB8500_IT_SOURCE7_REG		0x06
34 #define AB8500_IT_SOURCE8_REG		0x07
35 #define AB9540_IT_SOURCE13_REG		0x0C
36 #define AB8500_IT_SOURCE19_REG		0x12
37 #define AB8500_IT_SOURCE20_REG		0x13
38 #define AB8500_IT_SOURCE21_REG		0x14
39 #define AB8500_IT_SOURCE22_REG		0x15
40 #define AB8500_IT_SOURCE23_REG		0x16
41 #define AB8500_IT_SOURCE24_REG		0x17
42 
43 /*
44  * latch registers
45  */
46 #define AB8500_IT_LATCH1_REG		0x20
47 #define AB8500_IT_LATCH2_REG		0x21
48 #define AB8500_IT_LATCH3_REG		0x22
49 #define AB8500_IT_LATCH4_REG		0x23
50 #define AB8500_IT_LATCH5_REG		0x24
51 #define AB8500_IT_LATCH6_REG		0x25
52 #define AB8500_IT_LATCH7_REG		0x26
53 #define AB8500_IT_LATCH8_REG		0x27
54 #define AB8500_IT_LATCH9_REG		0x28
55 #define AB8500_IT_LATCH10_REG		0x29
56 #define AB8500_IT_LATCH12_REG		0x2B
57 #define AB9540_IT_LATCH13_REG		0x2C
58 #define AB8500_IT_LATCH19_REG		0x32
59 #define AB8500_IT_LATCH20_REG		0x33
60 #define AB8500_IT_LATCH21_REG		0x34
61 #define AB8500_IT_LATCH22_REG		0x35
62 #define AB8500_IT_LATCH23_REG		0x36
63 #define AB8500_IT_LATCH24_REG		0x37
64 
65 /*
66  * mask registers
67  */
68 
69 #define AB8500_IT_MASK1_REG		0x40
70 #define AB8500_IT_MASK2_REG		0x41
71 #define AB8500_IT_MASK3_REG		0x42
72 #define AB8500_IT_MASK4_REG		0x43
73 #define AB8500_IT_MASK5_REG		0x44
74 #define AB8500_IT_MASK6_REG		0x45
75 #define AB8500_IT_MASK7_REG		0x46
76 #define AB8500_IT_MASK8_REG		0x47
77 #define AB8500_IT_MASK9_REG		0x48
78 #define AB8500_IT_MASK10_REG		0x49
79 #define AB8500_IT_MASK11_REG		0x4A
80 #define AB8500_IT_MASK12_REG		0x4B
81 #define AB8500_IT_MASK13_REG		0x4C
82 #define AB8500_IT_MASK14_REG		0x4D
83 #define AB8500_IT_MASK15_REG		0x4E
84 #define AB8500_IT_MASK16_REG		0x4F
85 #define AB8500_IT_MASK17_REG		0x50
86 #define AB8500_IT_MASK18_REG		0x51
87 #define AB8500_IT_MASK19_REG		0x52
88 #define AB8500_IT_MASK20_REG		0x53
89 #define AB8500_IT_MASK21_REG		0x54
90 #define AB8500_IT_MASK22_REG		0x55
91 #define AB8500_IT_MASK23_REG		0x56
92 #define AB8500_IT_MASK24_REG		0x57
93 
94 #define AB8500_REV_REG			0x80
95 #define AB8500_IC_NAME_REG		0x82
96 #define AB8500_SWITCH_OFF_STATUS	0x00
97 
98 #define AB8500_TURN_ON_STATUS		0x00
99 
100 #define AB9540_MODEM_CTRL2_REG			0x23
101 #define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT	BIT(2)
102 
103 /*
104  * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
105  * numbers are indexed into this array with (num / 8). The interupts are
106  * defined in linux/mfd/ab8500.h
107  *
108  * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
109  * offset 0.
110  */
111 /* AB8500 support */
112 static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
113 	0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
114 };
115 
116 /* AB9540 support */
117 static const int ab9540_irq_regoffset[AB9540_NUM_IRQ_REGS] = {
118 	0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24,
119 };
120 
121 static const char ab8500_version_str[][7] = {
122 	[AB8500_VERSION_AB8500] = "AB8500",
123 	[AB8500_VERSION_AB8505] = "AB8505",
124 	[AB8500_VERSION_AB9540] = "AB9540",
125 	[AB8500_VERSION_AB8540] = "AB8540",
126 };
127 
ab8500_get_chip_id(struct device * dev)128 static int ab8500_get_chip_id(struct device *dev)
129 {
130 	struct ab8500 *ab8500;
131 
132 	if (!dev)
133 		return -EINVAL;
134 	ab8500 = dev_get_drvdata(dev->parent);
135 	return ab8500 ? (int)ab8500->chip_id : -EINVAL;
136 }
137 
set_register_interruptible(struct ab8500 * ab8500,u8 bank,u8 reg,u8 data)138 static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
139 	u8 reg, u8 data)
140 {
141 	int ret;
142 	/*
143 	 * Put the u8 bank and u8 register together into a an u16.
144 	 * The bank on higher 8 bits and register in lower 8 bits.
145 	 * */
146 	u16 addr = ((u16)bank) << 8 | reg;
147 
148 	dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
149 
150 	mutex_lock(&ab8500->lock);
151 
152 	ret = ab8500->write(ab8500, addr, data);
153 	if (ret < 0)
154 		dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
155 			addr, ret);
156 	mutex_unlock(&ab8500->lock);
157 
158 	return ret;
159 }
160 
ab8500_set_register(struct device * dev,u8 bank,u8 reg,u8 value)161 static int ab8500_set_register(struct device *dev, u8 bank,
162 	u8 reg, u8 value)
163 {
164 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
165 
166 	return set_register_interruptible(ab8500, bank, reg, value);
167 }
168 
get_register_interruptible(struct ab8500 * ab8500,u8 bank,u8 reg,u8 * value)169 static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
170 	u8 reg, u8 *value)
171 {
172 	int ret;
173 	/* put the u8 bank and u8 reg together into a an u16.
174 	 * bank on higher 8 bits and reg in lower */
175 	u16 addr = ((u16)bank) << 8 | reg;
176 
177 	mutex_lock(&ab8500->lock);
178 
179 	ret = ab8500->read(ab8500, addr);
180 	if (ret < 0)
181 		dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
182 			addr, ret);
183 	else
184 		*value = ret;
185 
186 	mutex_unlock(&ab8500->lock);
187 	dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
188 
189 	return ret;
190 }
191 
ab8500_get_register(struct device * dev,u8 bank,u8 reg,u8 * value)192 static int ab8500_get_register(struct device *dev, u8 bank,
193 	u8 reg, u8 *value)
194 {
195 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
196 
197 	return get_register_interruptible(ab8500, bank, reg, value);
198 }
199 
mask_and_set_register_interruptible(struct ab8500 * ab8500,u8 bank,u8 reg,u8 bitmask,u8 bitvalues)200 static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
201 	u8 reg, u8 bitmask, u8 bitvalues)
202 {
203 	int ret;
204 	/* put the u8 bank and u8 reg together into a an u16.
205 	 * bank on higher 8 bits and reg in lower */
206 	u16 addr = ((u16)bank) << 8 | reg;
207 
208 	mutex_lock(&ab8500->lock);
209 
210 	if (ab8500->write_masked == NULL) {
211 		u8 data;
212 
213 		ret = ab8500->read(ab8500, addr);
214 		if (ret < 0) {
215 			dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
216 				addr, ret);
217 			goto out;
218 		}
219 
220 		data = (u8)ret;
221 		data = (~bitmask & data) | (bitmask & bitvalues);
222 
223 		ret = ab8500->write(ab8500, addr, data);
224 		if (ret < 0)
225 			dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
226 				addr, ret);
227 
228 		dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr,
229 			data);
230 		goto out;
231 	}
232 	ret = ab8500->write_masked(ab8500, addr, bitmask, bitvalues);
233 	if (ret < 0)
234 		dev_err(ab8500->dev, "failed to modify reg %#x: %d\n", addr,
235 			ret);
236 out:
237 	mutex_unlock(&ab8500->lock);
238 	return ret;
239 }
240 
ab8500_mask_and_set_register(struct device * dev,u8 bank,u8 reg,u8 bitmask,u8 bitvalues)241 static int ab8500_mask_and_set_register(struct device *dev,
242 	u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
243 {
244 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
245 
246 	return mask_and_set_register_interruptible(ab8500, bank, reg,
247 		bitmask, bitvalues);
248 
249 }
250 
251 static struct abx500_ops ab8500_ops = {
252 	.get_chip_id = ab8500_get_chip_id,
253 	.get_register = ab8500_get_register,
254 	.set_register = ab8500_set_register,
255 	.get_register_page = NULL,
256 	.set_register_page = NULL,
257 	.mask_and_set_register = ab8500_mask_and_set_register,
258 	.event_registers_startup_state_get = NULL,
259 	.startup_irq_enabled = NULL,
260 };
261 
ab8500_irq_lock(struct irq_data * data)262 static void ab8500_irq_lock(struct irq_data *data)
263 {
264 	struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
265 
266 	mutex_lock(&ab8500->irq_lock);
267 }
268 
ab8500_irq_sync_unlock(struct irq_data * data)269 static void ab8500_irq_sync_unlock(struct irq_data *data)
270 {
271 	struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
272 	int i;
273 
274 	for (i = 0; i < ab8500->mask_size; i++) {
275 		u8 old = ab8500->oldmask[i];
276 		u8 new = ab8500->mask[i];
277 		int reg;
278 
279 		if (new == old)
280 			continue;
281 
282 		/*
283 		 * Interrupt register 12 doesn't exist prior to AB8500 version
284 		 * 2.0
285 		 */
286 		if (ab8500->irq_reg_offset[i] == 11 &&
287 			is_ab8500_1p1_or_earlier(ab8500))
288 			continue;
289 
290 		ab8500->oldmask[i] = new;
291 
292 		reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
293 		set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
294 	}
295 
296 	mutex_unlock(&ab8500->irq_lock);
297 }
298 
ab8500_irq_mask(struct irq_data * data)299 static void ab8500_irq_mask(struct irq_data *data)
300 {
301 	struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
302 	int offset = data->irq - ab8500->irq_base;
303 	int index = offset / 8;
304 	int mask = 1 << (offset % 8);
305 
306 	ab8500->mask[index] |= mask;
307 }
308 
ab8500_irq_unmask(struct irq_data * data)309 static void ab8500_irq_unmask(struct irq_data *data)
310 {
311 	struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
312 	int offset = data->irq - ab8500->irq_base;
313 	int index = offset / 8;
314 	int mask = 1 << (offset % 8);
315 
316 	ab8500->mask[index] &= ~mask;
317 }
318 
319 static struct irq_chip ab8500_irq_chip = {
320 	.name			= "ab8500",
321 	.irq_bus_lock		= ab8500_irq_lock,
322 	.irq_bus_sync_unlock	= ab8500_irq_sync_unlock,
323 	.irq_mask		= ab8500_irq_mask,
324 	.irq_disable		= ab8500_irq_mask,
325 	.irq_unmask		= ab8500_irq_unmask,
326 };
327 
ab8500_irq(int irq,void * dev)328 static irqreturn_t ab8500_irq(int irq, void *dev)
329 {
330 	struct ab8500 *ab8500 = dev;
331 	int i;
332 
333 	dev_vdbg(ab8500->dev, "interrupt\n");
334 
335 	for (i = 0; i < ab8500->mask_size; i++) {
336 		int regoffset = ab8500->irq_reg_offset[i];
337 		int status;
338 		u8 value;
339 
340 		/*
341 		 * Interrupt register 12 doesn't exist prior to AB8500 version
342 		 * 2.0
343 		 */
344 		if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
345 			continue;
346 
347 		status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
348 			AB8500_IT_LATCH1_REG + regoffset, &value);
349 		if (status < 0 || value == 0)
350 			continue;
351 
352 		do {
353 			int bit = __ffs(value);
354 			int line = i * 8 + bit;
355 
356 			handle_nested_irq(ab8500->irq_base + line);
357 			value &= ~(1 << bit);
358 		} while (value);
359 	}
360 
361 	return IRQ_HANDLED;
362 }
363 
ab8500_irq_init(struct ab8500 * ab8500)364 static int ab8500_irq_init(struct ab8500 *ab8500)
365 {
366 	int base = ab8500->irq_base;
367 	int irq;
368 	int num_irqs;
369 
370 	if (is_ab9540(ab8500))
371 		num_irqs = AB9540_NR_IRQS;
372 	else if (is_ab8505(ab8500))
373 		num_irqs = AB8505_NR_IRQS;
374 	else
375 		num_irqs = AB8500_NR_IRQS;
376 
377 	for (irq = base; irq < base + num_irqs; irq++) {
378 		irq_set_chip_data(irq, ab8500);
379 		irq_set_chip_and_handler(irq, &ab8500_irq_chip,
380 					 handle_simple_irq);
381 		irq_set_nested_thread(irq, 1);
382 #ifdef CONFIG_ARM
383 		set_irq_flags(irq, IRQF_VALID);
384 #else
385 		irq_set_noprobe(irq);
386 #endif
387 	}
388 
389 	return 0;
390 }
391 
ab8500_irq_remove(struct ab8500 * ab8500)392 static void ab8500_irq_remove(struct ab8500 *ab8500)
393 {
394 	int base = ab8500->irq_base;
395 	int irq;
396 	int num_irqs;
397 
398 	if (is_ab9540(ab8500))
399 		num_irqs = AB9540_NR_IRQS;
400 	else if (is_ab8505(ab8500))
401 		num_irqs = AB8505_NR_IRQS;
402 	else
403 		num_irqs = AB8500_NR_IRQS;
404 
405 	for (irq = base; irq < base + num_irqs; irq++) {
406 #ifdef CONFIG_ARM
407 		set_irq_flags(irq, 0);
408 #endif
409 		irq_set_chip_and_handler(irq, NULL, NULL);
410 		irq_set_chip_data(irq, NULL);
411 	}
412 }
413 
414 /* AB8500 GPIO Resources */
415 static struct resource __devinitdata ab8500_gpio_resources[] = {
416 	{
417 		.name	= "GPIO_INT6",
418 		.start	= AB8500_INT_GPIO6R,
419 		.end	= AB8500_INT_GPIO41F,
420 		.flags	= IORESOURCE_IRQ,
421 	}
422 };
423 
424 /* AB9540 GPIO Resources */
425 static struct resource __devinitdata ab9540_gpio_resources[] = {
426 	{
427 		.name	= "GPIO_INT6",
428 		.start	= AB8500_INT_GPIO6R,
429 		.end	= AB8500_INT_GPIO41F,
430 		.flags	= IORESOURCE_IRQ,
431 	},
432 	{
433 		.name	= "GPIO_INT14",
434 		.start	= AB9540_INT_GPIO50R,
435 		.end	= AB9540_INT_GPIO54R,
436 		.flags	= IORESOURCE_IRQ,
437 	},
438 	{
439 		.name	= "GPIO_INT15",
440 		.start	= AB9540_INT_GPIO50F,
441 		.end	= AB9540_INT_GPIO54F,
442 		.flags	= IORESOURCE_IRQ,
443 	}
444 };
445 
446 static struct resource __devinitdata ab8500_gpadc_resources[] = {
447 	{
448 		.name	= "HW_CONV_END",
449 		.start	= AB8500_INT_GP_HW_ADC_CONV_END,
450 		.end	= AB8500_INT_GP_HW_ADC_CONV_END,
451 		.flags	= IORESOURCE_IRQ,
452 	},
453 	{
454 		.name	= "SW_CONV_END",
455 		.start	= AB8500_INT_GP_SW_ADC_CONV_END,
456 		.end	= AB8500_INT_GP_SW_ADC_CONV_END,
457 		.flags	= IORESOURCE_IRQ,
458 	},
459 };
460 
461 static struct resource __devinitdata ab8500_rtc_resources[] = {
462 	{
463 		.name	= "60S",
464 		.start	= AB8500_INT_RTC_60S,
465 		.end	= AB8500_INT_RTC_60S,
466 		.flags	= IORESOURCE_IRQ,
467 	},
468 	{
469 		.name	= "ALARM",
470 		.start	= AB8500_INT_RTC_ALARM,
471 		.end	= AB8500_INT_RTC_ALARM,
472 		.flags	= IORESOURCE_IRQ,
473 	},
474 };
475 
476 static struct resource __devinitdata ab8500_poweronkey_db_resources[] = {
477 	{
478 		.name	= "ONKEY_DBF",
479 		.start	= AB8500_INT_PON_KEY1DB_F,
480 		.end	= AB8500_INT_PON_KEY1DB_F,
481 		.flags	= IORESOURCE_IRQ,
482 	},
483 	{
484 		.name	= "ONKEY_DBR",
485 		.start	= AB8500_INT_PON_KEY1DB_R,
486 		.end	= AB8500_INT_PON_KEY1DB_R,
487 		.flags	= IORESOURCE_IRQ,
488 	},
489 };
490 
491 static struct resource __devinitdata ab8500_av_acc_detect_resources[] = {
492 	{
493 	       .name = "ACC_DETECT_1DB_F",
494 	       .start = AB8500_INT_ACC_DETECT_1DB_F,
495 	       .end = AB8500_INT_ACC_DETECT_1DB_F,
496 	       .flags = IORESOURCE_IRQ,
497 	},
498 	{
499 	       .name = "ACC_DETECT_1DB_R",
500 	       .start = AB8500_INT_ACC_DETECT_1DB_R,
501 	       .end = AB8500_INT_ACC_DETECT_1DB_R,
502 	       .flags = IORESOURCE_IRQ,
503 	},
504 	{
505 	       .name = "ACC_DETECT_21DB_F",
506 	       .start = AB8500_INT_ACC_DETECT_21DB_F,
507 	       .end = AB8500_INT_ACC_DETECT_21DB_F,
508 	       .flags = IORESOURCE_IRQ,
509 	},
510 	{
511 	       .name = "ACC_DETECT_21DB_R",
512 	       .start = AB8500_INT_ACC_DETECT_21DB_R,
513 	       .end = AB8500_INT_ACC_DETECT_21DB_R,
514 	       .flags = IORESOURCE_IRQ,
515 	},
516 	{
517 	       .name = "ACC_DETECT_22DB_F",
518 	       .start = AB8500_INT_ACC_DETECT_22DB_F,
519 	       .end = AB8500_INT_ACC_DETECT_22DB_F,
520 	       .flags = IORESOURCE_IRQ,
521 	},
522 	{
523 	       .name = "ACC_DETECT_22DB_R",
524 	       .start = AB8500_INT_ACC_DETECT_22DB_R,
525 	       .end = AB8500_INT_ACC_DETECT_22DB_R,
526 	       .flags = IORESOURCE_IRQ,
527 	},
528 };
529 
530 static struct resource __devinitdata ab8500_charger_resources[] = {
531 	{
532 		.name = "MAIN_CH_UNPLUG_DET",
533 		.start = AB8500_INT_MAIN_CH_UNPLUG_DET,
534 		.end = AB8500_INT_MAIN_CH_UNPLUG_DET,
535 		.flags = IORESOURCE_IRQ,
536 	},
537 	{
538 		.name = "MAIN_CHARGE_PLUG_DET",
539 		.start = AB8500_INT_MAIN_CH_PLUG_DET,
540 		.end = AB8500_INT_MAIN_CH_PLUG_DET,
541 		.flags = IORESOURCE_IRQ,
542 	},
543 	{
544 		.name = "VBUS_DET_R",
545 		.start = AB8500_INT_VBUS_DET_R,
546 		.end = AB8500_INT_VBUS_DET_R,
547 		.flags = IORESOURCE_IRQ,
548 	},
549 	{
550 		.name = "VBUS_DET_F",
551 		.start = AB8500_INT_VBUS_DET_F,
552 		.end = AB8500_INT_VBUS_DET_F,
553 		.flags = IORESOURCE_IRQ,
554 	},
555 	{
556 		.name = "USB_LINK_STATUS",
557 		.start = AB8500_INT_USB_LINK_STATUS,
558 		.end = AB8500_INT_USB_LINK_STATUS,
559 		.flags = IORESOURCE_IRQ,
560 	},
561 	{
562 		.name = "VBUS_OVV",
563 		.start = AB8500_INT_VBUS_OVV,
564 		.end = AB8500_INT_VBUS_OVV,
565 		.flags = IORESOURCE_IRQ,
566 	},
567 	{
568 		.name = "USB_CH_TH_PROT_R",
569 		.start = AB8500_INT_USB_CH_TH_PROT_R,
570 		.end = AB8500_INT_USB_CH_TH_PROT_R,
571 		.flags = IORESOURCE_IRQ,
572 	},
573 	{
574 		.name = "USB_CH_TH_PROT_F",
575 		.start = AB8500_INT_USB_CH_TH_PROT_F,
576 		.end = AB8500_INT_USB_CH_TH_PROT_F,
577 		.flags = IORESOURCE_IRQ,
578 	},
579 	{
580 		.name = "MAIN_EXT_CH_NOT_OK",
581 		.start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
582 		.end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
583 		.flags = IORESOURCE_IRQ,
584 	},
585 	{
586 		.name = "MAIN_CH_TH_PROT_R",
587 		.start = AB8500_INT_MAIN_CH_TH_PROT_R,
588 		.end = AB8500_INT_MAIN_CH_TH_PROT_R,
589 		.flags = IORESOURCE_IRQ,
590 	},
591 	{
592 		.name = "MAIN_CH_TH_PROT_F",
593 		.start = AB8500_INT_MAIN_CH_TH_PROT_F,
594 		.end = AB8500_INT_MAIN_CH_TH_PROT_F,
595 		.flags = IORESOURCE_IRQ,
596 	},
597 	{
598 		.name = "USB_CHARGER_NOT_OKR",
599 		.start = AB8500_INT_USB_CHARGER_NOT_OKR,
600 		.end = AB8500_INT_USB_CHARGER_NOT_OKR,
601 		.flags = IORESOURCE_IRQ,
602 	},
603 	{
604 		.name = "CH_WD_EXP",
605 		.start = AB8500_INT_CH_WD_EXP,
606 		.end = AB8500_INT_CH_WD_EXP,
607 		.flags = IORESOURCE_IRQ,
608 	},
609 };
610 
611 static struct resource __devinitdata ab8500_btemp_resources[] = {
612 	{
613 		.name = "BAT_CTRL_INDB",
614 		.start = AB8500_INT_BAT_CTRL_INDB,
615 		.end = AB8500_INT_BAT_CTRL_INDB,
616 		.flags = IORESOURCE_IRQ,
617 	},
618 	{
619 		.name = "BTEMP_LOW",
620 		.start = AB8500_INT_BTEMP_LOW,
621 		.end = AB8500_INT_BTEMP_LOW,
622 		.flags = IORESOURCE_IRQ,
623 	},
624 	{
625 		.name = "BTEMP_HIGH",
626 		.start = AB8500_INT_BTEMP_HIGH,
627 		.end = AB8500_INT_BTEMP_HIGH,
628 		.flags = IORESOURCE_IRQ,
629 	},
630 	{
631 		.name = "BTEMP_LOW_MEDIUM",
632 		.start = AB8500_INT_BTEMP_LOW_MEDIUM,
633 		.end = AB8500_INT_BTEMP_LOW_MEDIUM,
634 		.flags = IORESOURCE_IRQ,
635 	},
636 	{
637 		.name = "BTEMP_MEDIUM_HIGH",
638 		.start = AB8500_INT_BTEMP_MEDIUM_HIGH,
639 		.end = AB8500_INT_BTEMP_MEDIUM_HIGH,
640 		.flags = IORESOURCE_IRQ,
641 	},
642 };
643 
644 static struct resource __devinitdata ab8500_fg_resources[] = {
645 	{
646 		.name = "NCONV_ACCU",
647 		.start = AB8500_INT_CCN_CONV_ACC,
648 		.end = AB8500_INT_CCN_CONV_ACC,
649 		.flags = IORESOURCE_IRQ,
650 	},
651 	{
652 		.name = "BATT_OVV",
653 		.start = AB8500_INT_BATT_OVV,
654 		.end = AB8500_INT_BATT_OVV,
655 		.flags = IORESOURCE_IRQ,
656 	},
657 	{
658 		.name = "LOW_BAT_F",
659 		.start = AB8500_INT_LOW_BAT_F,
660 		.end = AB8500_INT_LOW_BAT_F,
661 		.flags = IORESOURCE_IRQ,
662 	},
663 	{
664 		.name = "LOW_BAT_R",
665 		.start = AB8500_INT_LOW_BAT_R,
666 		.end = AB8500_INT_LOW_BAT_R,
667 		.flags = IORESOURCE_IRQ,
668 	},
669 	{
670 		.name = "CC_INT_CALIB",
671 		.start = AB8500_INT_CC_INT_CALIB,
672 		.end = AB8500_INT_CC_INT_CALIB,
673 		.flags = IORESOURCE_IRQ,
674 	},
675 	{
676 		.name = "CCEOC",
677 		.start = AB8500_INT_CCEOC,
678 		.end = AB8500_INT_CCEOC,
679 		.flags = IORESOURCE_IRQ,
680 	},
681 };
682 
683 static struct resource __devinitdata ab8500_chargalg_resources[] = {};
684 
685 #ifdef CONFIG_DEBUG_FS
686 static struct resource __devinitdata ab8500_debug_resources[] = {
687 	{
688 		.name	= "IRQ_FIRST",
689 		.start	= AB8500_INT_MAIN_EXT_CH_NOT_OK,
690 		.end	= AB8500_INT_MAIN_EXT_CH_NOT_OK,
691 		.flags	= IORESOURCE_IRQ,
692 	},
693 	{
694 		.name	= "IRQ_LAST",
695 		.start	= AB8500_INT_XTAL32K_KO,
696 		.end	= AB8500_INT_XTAL32K_KO,
697 		.flags	= IORESOURCE_IRQ,
698 	},
699 };
700 #endif
701 
702 static struct resource __devinitdata ab8500_usb_resources[] = {
703 	{
704 		.name = "ID_WAKEUP_R",
705 		.start = AB8500_INT_ID_WAKEUP_R,
706 		.end = AB8500_INT_ID_WAKEUP_R,
707 		.flags = IORESOURCE_IRQ,
708 	},
709 	{
710 		.name = "ID_WAKEUP_F",
711 		.start = AB8500_INT_ID_WAKEUP_F,
712 		.end = AB8500_INT_ID_WAKEUP_F,
713 		.flags = IORESOURCE_IRQ,
714 	},
715 	{
716 		.name = "VBUS_DET_F",
717 		.start = AB8500_INT_VBUS_DET_F,
718 		.end = AB8500_INT_VBUS_DET_F,
719 		.flags = IORESOURCE_IRQ,
720 	},
721 	{
722 		.name = "VBUS_DET_R",
723 		.start = AB8500_INT_VBUS_DET_R,
724 		.end = AB8500_INT_VBUS_DET_R,
725 		.flags = IORESOURCE_IRQ,
726 	},
727 	{
728 		.name = "USB_LINK_STATUS",
729 		.start = AB8500_INT_USB_LINK_STATUS,
730 		.end = AB8500_INT_USB_LINK_STATUS,
731 		.flags = IORESOURCE_IRQ,
732 	},
733 	{
734 		.name = "USB_ADP_PROBE_PLUG",
735 		.start = AB8500_INT_ADP_PROBE_PLUG,
736 		.end = AB8500_INT_ADP_PROBE_PLUG,
737 		.flags = IORESOURCE_IRQ,
738 	},
739 	{
740 		.name = "USB_ADP_PROBE_UNPLUG",
741 		.start = AB8500_INT_ADP_PROBE_UNPLUG,
742 		.end = AB8500_INT_ADP_PROBE_UNPLUG,
743 		.flags = IORESOURCE_IRQ,
744 	},
745 };
746 
747 static struct resource __devinitdata ab8500_temp_resources[] = {
748 	{
749 		.name  = "AB8500_TEMP_WARM",
750 		.start = AB8500_INT_TEMP_WARM,
751 		.end   = AB8500_INT_TEMP_WARM,
752 		.flags = IORESOURCE_IRQ,
753 	},
754 };
755 
756 static struct mfd_cell __devinitdata abx500_common_devs[] = {
757 #ifdef CONFIG_DEBUG_FS
758 	{
759 		.name = "ab8500-debug",
760 		.num_resources = ARRAY_SIZE(ab8500_debug_resources),
761 		.resources = ab8500_debug_resources,
762 	},
763 #endif
764 	{
765 		.name = "ab8500-sysctrl",
766 	},
767 	{
768 		.name = "ab8500-regulator",
769 	},
770 	{
771 		.name = "ab8500-gpadc",
772 		.num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
773 		.resources = ab8500_gpadc_resources,
774 	},
775 	{
776 		.name = "ab8500-rtc",
777 		.num_resources = ARRAY_SIZE(ab8500_rtc_resources),
778 		.resources = ab8500_rtc_resources,
779 	},
780 	{
781 		.name = "ab8500-charger",
782 		.num_resources = ARRAY_SIZE(ab8500_charger_resources),
783 		.resources = ab8500_charger_resources,
784 	},
785 	{
786 		.name = "ab8500-btemp",
787 		.num_resources = ARRAY_SIZE(ab8500_btemp_resources),
788 		.resources = ab8500_btemp_resources,
789 	},
790 	{
791 		.name = "ab8500-fg",
792 		.num_resources = ARRAY_SIZE(ab8500_fg_resources),
793 		.resources = ab8500_fg_resources,
794 	},
795 	{
796 		.name = "ab8500-chargalg",
797 		.num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
798 		.resources = ab8500_chargalg_resources,
799 	},
800 	{
801 		.name = "ab8500-acc-det",
802 		.num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
803 		.resources = ab8500_av_acc_detect_resources,
804 	},
805 	{
806 		.name = "ab8500-codec",
807 	},
808 
809 	{
810 		.name = "ab8500-poweron-key",
811 		.num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
812 		.resources = ab8500_poweronkey_db_resources,
813 	},
814 	{
815 		.name = "ab8500-pwm",
816 		.id = 1,
817 	},
818 	{
819 		.name = "ab8500-pwm",
820 		.id = 2,
821 	},
822 	{
823 		.name = "ab8500-pwm",
824 		.id = 3,
825 	},
826 	{ .name = "ab8500-leds", },
827 	{
828 		.name = "ab8500-denc",
829 	},
830 	{
831 		.name = "ab8500-temp",
832 		.num_resources = ARRAY_SIZE(ab8500_temp_resources),
833 		.resources = ab8500_temp_resources,
834 	},
835 };
836 
837 static struct mfd_cell __devinitdata ab8500_devs[] = {
838 	{
839 		.name = "ab8500-gpio",
840 		.num_resources = ARRAY_SIZE(ab8500_gpio_resources),
841 		.resources = ab8500_gpio_resources,
842 	},
843 	{
844 		.name = "ab8500-usb",
845 		.num_resources = ARRAY_SIZE(ab8500_usb_resources),
846 		.resources = ab8500_usb_resources,
847 	},
848 };
849 
850 static struct mfd_cell __devinitdata ab9540_devs[] = {
851 	{
852 		.name = "ab8500-gpio",
853 		.num_resources = ARRAY_SIZE(ab9540_gpio_resources),
854 		.resources = ab9540_gpio_resources,
855 	},
856 	{
857 		.name = "ab9540-usb",
858 		.num_resources = ARRAY_SIZE(ab8500_usb_resources),
859 		.resources = ab8500_usb_resources,
860 	},
861 };
862 
show_chip_id(struct device * dev,struct device_attribute * attr,char * buf)863 static ssize_t show_chip_id(struct device *dev,
864 				struct device_attribute *attr, char *buf)
865 {
866 	struct ab8500 *ab8500;
867 
868 	ab8500 = dev_get_drvdata(dev);
869 	return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
870 }
871 
872 /*
873  * ab8500 has switched off due to (SWITCH_OFF_STATUS):
874  * 0x01 Swoff bit programming
875  * 0x02 Thermal protection activation
876  * 0x04 Vbat lower then BattOk falling threshold
877  * 0x08 Watchdog expired
878  * 0x10 Non presence of 32kHz clock
879  * 0x20 Battery level lower than power on reset threshold
880  * 0x40 Power on key 1 pressed longer than 10 seconds
881  * 0x80 DB8500 thermal shutdown
882  */
show_switch_off_status(struct device * dev,struct device_attribute * attr,char * buf)883 static ssize_t show_switch_off_status(struct device *dev,
884 				struct device_attribute *attr, char *buf)
885 {
886 	int ret;
887 	u8 value;
888 	struct ab8500 *ab8500;
889 
890 	ab8500 = dev_get_drvdata(dev);
891 	ret = get_register_interruptible(ab8500, AB8500_RTC,
892 		AB8500_SWITCH_OFF_STATUS, &value);
893 	if (ret < 0)
894 		return ret;
895 	return sprintf(buf, "%#x\n", value);
896 }
897 
898 /*
899  * ab8500 has turned on due to (TURN_ON_STATUS):
900  * 0x01 PORnVbat
901  * 0x02 PonKey1dbF
902  * 0x04 PonKey2dbF
903  * 0x08 RTCAlarm
904  * 0x10 MainChDet
905  * 0x20 VbusDet
906  * 0x40 UsbIDDetect
907  * 0x80 Reserved
908  */
show_turn_on_status(struct device * dev,struct device_attribute * attr,char * buf)909 static ssize_t show_turn_on_status(struct device *dev,
910 				struct device_attribute *attr, char *buf)
911 {
912 	int ret;
913 	u8 value;
914 	struct ab8500 *ab8500;
915 
916 	ab8500 = dev_get_drvdata(dev);
917 	ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
918 		AB8500_TURN_ON_STATUS, &value);
919 	if (ret < 0)
920 		return ret;
921 	return sprintf(buf, "%#x\n", value);
922 }
923 
show_ab9540_dbbrstn(struct device * dev,struct device_attribute * attr,char * buf)924 static ssize_t show_ab9540_dbbrstn(struct device *dev,
925 				struct device_attribute *attr, char *buf)
926 {
927 	struct ab8500 *ab8500;
928 	int ret;
929 	u8 value;
930 
931 	ab8500 = dev_get_drvdata(dev);
932 
933 	ret = get_register_interruptible(ab8500, AB8500_REGU_CTRL2,
934 		AB9540_MODEM_CTRL2_REG, &value);
935 	if (ret < 0)
936 		return ret;
937 
938 	return sprintf(buf, "%d\n",
939 			(value & AB9540_MODEM_CTRL2_SWDBBRSTN_BIT) ? 1 : 0);
940 }
941 
store_ab9540_dbbrstn(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)942 static ssize_t store_ab9540_dbbrstn(struct device *dev,
943 	struct device_attribute *attr, const char *buf, size_t count)
944 {
945 	struct ab8500 *ab8500;
946 	int ret = count;
947 	int err;
948 	u8 bitvalues;
949 
950 	ab8500 = dev_get_drvdata(dev);
951 
952 	if (count > 0) {
953 		switch (buf[0]) {
954 		case '0':
955 			bitvalues = 0;
956 			break;
957 		case '1':
958 			bitvalues = AB9540_MODEM_CTRL2_SWDBBRSTN_BIT;
959 			break;
960 		default:
961 			goto exit;
962 		}
963 
964 		err = mask_and_set_register_interruptible(ab8500,
965 			AB8500_REGU_CTRL2, AB9540_MODEM_CTRL2_REG,
966 			AB9540_MODEM_CTRL2_SWDBBRSTN_BIT, bitvalues);
967 		if (err)
968 			dev_info(ab8500->dev,
969 				"Failed to set DBBRSTN %c, err %#x\n",
970 				buf[0], err);
971 	}
972 
973 exit:
974 	return ret;
975 }
976 
977 static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
978 static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
979 static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
980 static DEVICE_ATTR(dbbrstn, S_IRUGO | S_IWUSR,
981 			show_ab9540_dbbrstn, store_ab9540_dbbrstn);
982 
983 static struct attribute *ab8500_sysfs_entries[] = {
984 	&dev_attr_chip_id.attr,
985 	&dev_attr_switch_off_status.attr,
986 	&dev_attr_turn_on_status.attr,
987 	NULL,
988 };
989 
990 static struct attribute *ab9540_sysfs_entries[] = {
991 	&dev_attr_chip_id.attr,
992 	&dev_attr_switch_off_status.attr,
993 	&dev_attr_turn_on_status.attr,
994 	&dev_attr_dbbrstn.attr,
995 	NULL,
996 };
997 
998 static struct attribute_group ab8500_attr_group = {
999 	.attrs	= ab8500_sysfs_entries,
1000 };
1001 
1002 static struct attribute_group ab9540_attr_group = {
1003 	.attrs	= ab9540_sysfs_entries,
1004 };
1005 
ab8500_init(struct ab8500 * ab8500,enum ab8500_version version)1006 int __devinit ab8500_init(struct ab8500 *ab8500, enum ab8500_version version)
1007 {
1008 	struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
1009 	int ret;
1010 	int i;
1011 	u8 value;
1012 
1013 	if (plat)
1014 		ab8500->irq_base = plat->irq_base;
1015 
1016 	mutex_init(&ab8500->lock);
1017 	mutex_init(&ab8500->irq_lock);
1018 
1019 	if (version != AB8500_VERSION_UNDEFINED)
1020 		ab8500->version = version;
1021 	else {
1022 		ret = get_register_interruptible(ab8500, AB8500_MISC,
1023 			AB8500_IC_NAME_REG, &value);
1024 		if (ret < 0)
1025 			return ret;
1026 
1027 		ab8500->version = value;
1028 	}
1029 
1030 	ret = get_register_interruptible(ab8500, AB8500_MISC,
1031 		AB8500_REV_REG, &value);
1032 	if (ret < 0)
1033 		return ret;
1034 
1035 	ab8500->chip_id = value;
1036 
1037 	dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
1038 			ab8500_version_str[ab8500->version],
1039 			ab8500->chip_id >> 4,
1040 			ab8500->chip_id & 0x0F);
1041 
1042 	/* Configure AB8500 or AB9540 IRQ */
1043 	if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
1044 		ab8500->mask_size = AB9540_NUM_IRQ_REGS;
1045 		ab8500->irq_reg_offset = ab9540_irq_regoffset;
1046 	} else {
1047 		ab8500->mask_size = AB8500_NUM_IRQ_REGS;
1048 		ab8500->irq_reg_offset = ab8500_irq_regoffset;
1049 	}
1050 	ab8500->mask = kzalloc(ab8500->mask_size, GFP_KERNEL);
1051 	if (!ab8500->mask)
1052 		return -ENOMEM;
1053 	ab8500->oldmask = kzalloc(ab8500->mask_size, GFP_KERNEL);
1054 	if (!ab8500->oldmask) {
1055 		ret = -ENOMEM;
1056 		goto out_freemask;
1057 	}
1058 	/*
1059 	 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1060 	 * 0x01 Swoff bit programming
1061 	 * 0x02 Thermal protection activation
1062 	 * 0x04 Vbat lower then BattOk falling threshold
1063 	 * 0x08 Watchdog expired
1064 	 * 0x10 Non presence of 32kHz clock
1065 	 * 0x20 Battery level lower than power on reset threshold
1066 	 * 0x40 Power on key 1 pressed longer than 10 seconds
1067 	 * 0x80 DB8500 thermal shutdown
1068 	 */
1069 
1070 	ret = get_register_interruptible(ab8500, AB8500_RTC,
1071 		AB8500_SWITCH_OFF_STATUS, &value);
1072 	if (ret < 0)
1073 		return ret;
1074 	dev_info(ab8500->dev, "switch off status: %#x", value);
1075 
1076 	if (plat && plat->init)
1077 		plat->init(ab8500);
1078 
1079 	/* Clear and mask all interrupts */
1080 	for (i = 0; i < ab8500->mask_size; i++) {
1081 		/*
1082 		 * Interrupt register 12 doesn't exist prior to AB8500 version
1083 		 * 2.0
1084 		 */
1085 		if (ab8500->irq_reg_offset[i] == 11 &&
1086 				is_ab8500_1p1_or_earlier(ab8500))
1087 			continue;
1088 
1089 		get_register_interruptible(ab8500, AB8500_INTERRUPT,
1090 			AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
1091 			&value);
1092 		set_register_interruptible(ab8500, AB8500_INTERRUPT,
1093 			AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
1094 	}
1095 
1096 	ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
1097 	if (ret)
1098 		goto out_freeoldmask;
1099 
1100 	for (i = 0; i < ab8500->mask_size; i++)
1101 		ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
1102 
1103 	if (ab8500->irq_base) {
1104 		ret = ab8500_irq_init(ab8500);
1105 		if (ret)
1106 			goto out_freeoldmask;
1107 
1108 		ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
1109 					   IRQF_ONESHOT | IRQF_NO_SUSPEND,
1110 					   "ab8500", ab8500);
1111 		if (ret)
1112 			goto out_removeirq;
1113 	}
1114 
1115 	ret = mfd_add_devices(ab8500->dev, 0, abx500_common_devs,
1116 			      ARRAY_SIZE(abx500_common_devs), NULL,
1117 			      ab8500->irq_base);
1118 
1119 	if (ret)
1120 		goto out_freeirq;
1121 
1122 	if (is_ab9540(ab8500))
1123 		ret = mfd_add_devices(ab8500->dev, 0, ab9540_devs,
1124 			      ARRAY_SIZE(ab9540_devs), NULL,
1125 			      ab8500->irq_base);
1126 	else
1127 		ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
1128 			      ARRAY_SIZE(ab9540_devs), NULL,
1129 			      ab8500->irq_base);
1130 	if (ret)
1131 		goto out_freeirq;
1132 
1133 	if (is_ab9540(ab8500))
1134 		ret = sysfs_create_group(&ab8500->dev->kobj,
1135 					&ab9540_attr_group);
1136 	else
1137 		ret = sysfs_create_group(&ab8500->dev->kobj,
1138 					&ab8500_attr_group);
1139 	if (ret)
1140 		dev_err(ab8500->dev, "error creating sysfs entries\n");
1141 	else
1142 		return ret;
1143 
1144 out_freeirq:
1145 	if (ab8500->irq_base)
1146 		free_irq(ab8500->irq, ab8500);
1147 out_removeirq:
1148 	if (ab8500->irq_base)
1149 		ab8500_irq_remove(ab8500);
1150 out_freeoldmask:
1151 	kfree(ab8500->oldmask);
1152 out_freemask:
1153 	kfree(ab8500->mask);
1154 
1155 	return ret;
1156 }
1157 
ab8500_exit(struct ab8500 * ab8500)1158 int __devexit ab8500_exit(struct ab8500 *ab8500)
1159 {
1160 	if (is_ab9540(ab8500))
1161 		sysfs_remove_group(&ab8500->dev->kobj, &ab9540_attr_group);
1162 	else
1163 		sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
1164 	mfd_remove_devices(ab8500->dev);
1165 	if (ab8500->irq_base) {
1166 		free_irq(ab8500->irq, ab8500);
1167 		ab8500_irq_remove(ab8500);
1168 	}
1169 	kfree(ab8500->oldmask);
1170 	kfree(ab8500->mask);
1171 
1172 	return 0;
1173 }
1174 
1175 MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
1176 MODULE_DESCRIPTION("AB8500 MFD core");
1177 MODULE_LICENSE("GPL v2");
1178