Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Base driver for Marvell 88PM8607
  3 *
  4 * Copyright (C) 2009 Marvell International Ltd.
  5 * 	Haojian Zhuang <haojian.zhuang@marvell.com>
 
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 */
 11
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 
 14#include <linux/i2c.h>
 15#include <linux/irq.h>
 16#include <linux/interrupt.h>
 
 
 
 17#include <linux/platform_device.h>
 
 
 18#include <linux/mfd/core.h>
 19#include <linux/mfd/88pm860x.h>
 20#include <linux/regulator/machine.h>
 
 21
 22#define INT_STATUS_NUM			3
 23
 24static struct resource bk_resources[] __devinitdata = {
 25	{PM8606_BACKLIGHT1, PM8606_BACKLIGHT1, "backlight-0", IORESOURCE_IO,},
 26	{PM8606_BACKLIGHT2, PM8606_BACKLIGHT2, "backlight-1", IORESOURCE_IO,},
 27	{PM8606_BACKLIGHT3, PM8606_BACKLIGHT3, "backlight-2", IORESOURCE_IO,},
 28};
 29
 30static struct resource led_resources[] __devinitdata = {
 31	{PM8606_LED1_RED,   PM8606_LED1_RED,   "led0-red",   IORESOURCE_IO,},
 32	{PM8606_LED1_GREEN, PM8606_LED1_GREEN, "led0-green", IORESOURCE_IO,},
 33	{PM8606_LED1_BLUE,  PM8606_LED1_BLUE,  "led0-blue",  IORESOURCE_IO,},
 34	{PM8606_LED2_RED,   PM8606_LED2_RED,   "led1-red",   IORESOURCE_IO,},
 35	{PM8606_LED2_GREEN, PM8606_LED2_GREEN, "led1-green", IORESOURCE_IO,},
 36	{PM8606_LED2_BLUE,  PM8606_LED2_BLUE,  "led1-blue",  IORESOURCE_IO,},
 37};
 38
 39static struct resource regulator_resources[] __devinitdata = {
 40	{PM8607_ID_BUCK1, PM8607_ID_BUCK1, "buck-1", IORESOURCE_IO,},
 41	{PM8607_ID_BUCK2, PM8607_ID_BUCK2, "buck-2", IORESOURCE_IO,},
 42	{PM8607_ID_BUCK3, PM8607_ID_BUCK3, "buck-3", IORESOURCE_IO,},
 43	{PM8607_ID_LDO1,  PM8607_ID_LDO1,  "ldo-01", IORESOURCE_IO,},
 44	{PM8607_ID_LDO2,  PM8607_ID_LDO2,  "ldo-02", IORESOURCE_IO,},
 45	{PM8607_ID_LDO3,  PM8607_ID_LDO3,  "ldo-03", IORESOURCE_IO,},
 46	{PM8607_ID_LDO4,  PM8607_ID_LDO4,  "ldo-04", IORESOURCE_IO,},
 47	{PM8607_ID_LDO5,  PM8607_ID_LDO5,  "ldo-05", IORESOURCE_IO,},
 48	{PM8607_ID_LDO6,  PM8607_ID_LDO6,  "ldo-06", IORESOURCE_IO,},
 49	{PM8607_ID_LDO7,  PM8607_ID_LDO7,  "ldo-07", IORESOURCE_IO,},
 50	{PM8607_ID_LDO8,  PM8607_ID_LDO8,  "ldo-08", IORESOURCE_IO,},
 51	{PM8607_ID_LDO9,  PM8607_ID_LDO9,  "ldo-09", IORESOURCE_IO,},
 52	{PM8607_ID_LDO10, PM8607_ID_LDO10, "ldo-10", IORESOURCE_IO,},
 53	{PM8607_ID_LDO11, PM8607_ID_LDO11, "ldo-11", IORESOURCE_IO,},
 54	{PM8607_ID_LDO12, PM8607_ID_LDO12, "ldo-12", IORESOURCE_IO,},
 55	{PM8607_ID_LDO13, PM8607_ID_LDO13, "ldo-13", IORESOURCE_IO,},
 56	{PM8607_ID_LDO14, PM8607_ID_LDO14, "ldo-14", IORESOURCE_IO,},
 57	{PM8607_ID_LDO15, PM8607_ID_LDO15, "ldo-15", IORESOURCE_IO,},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 58};
 59
 60static struct resource touch_resources[] __devinitdata = {
 61	{PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
 62};
 63
 64static struct resource onkey_resources[] __devinitdata = {
 65	{PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
 66};
 67
 68static struct resource codec_resources[] __devinitdata = {
 69	/* Headset microphone insertion or removal */
 70	{PM8607_IRQ_MICIN,   PM8607_IRQ_MICIN,   "micin",   IORESOURCE_IRQ,},
 71	/* Hook-switch press or release */
 72	{PM8607_IRQ_HOOK,    PM8607_IRQ_HOOK,    "hook",    IORESOURCE_IRQ,},
 73	/* Headset insertion or removal */
 74	{PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
 75	/* Audio short */
 76	{PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
 
 77};
 78
 79static struct resource battery_resources[] __devinitdata = {
 80	{PM8607_IRQ_CC,  PM8607_IRQ_CC,  "columb counter", IORESOURCE_IRQ,},
 81	{PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery",        IORESOURCE_IRQ,},
 82};
 83
 84static struct resource charger_resources[] __devinitdata = {
 85	{PM8607_IRQ_CHG,  PM8607_IRQ_CHG,  "charger detect",  IORESOURCE_IRQ,},
 86	{PM8607_IRQ_CHG_DONE,  PM8607_IRQ_CHG_DONE,  "charging done",       IORESOURCE_IRQ,},
 87	{PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout",    IORESOURCE_IRQ,},
 88	{PM8607_IRQ_GPADC1,    PM8607_IRQ_GPADC1,    "battery temperature", IORESOURCE_IRQ,},
 
 
 
 
 
 89	{PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
 90	{PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage",    IORESOURCE_IRQ,},
 91};
 92
 93static struct resource rtc_resources[] __devinitdata = {
 94	{PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
 95};
 96
 97static struct mfd_cell bk_devs[] = {
 98	{"88pm860x-backlight", 0,},
 99	{"88pm860x-backlight", 1,},
100	{"88pm860x-backlight", 2,},
 
 
 
 
 
 
 
 
 
 
 
 
 
101};
102
103static struct mfd_cell led_devs[] = {
104	{"88pm860x-led", 0,},
105	{"88pm860x-led", 1,},
106	{"88pm860x-led", 2,},
107	{"88pm860x-led", 3,},
108	{"88pm860x-led", 4,},
109	{"88pm860x-led", 5,},
110};
111
112static struct mfd_cell regulator_devs[] = {
113	{"88pm860x-regulator", 0,},
114	{"88pm860x-regulator", 1,},
115	{"88pm860x-regulator", 2,},
116	{"88pm860x-regulator", 3,},
117	{"88pm860x-regulator", 4,},
118	{"88pm860x-regulator", 5,},
119	{"88pm860x-regulator", 6,},
120	{"88pm860x-regulator", 7,},
121	{"88pm860x-regulator", 8,},
122	{"88pm860x-regulator", 9,},
123	{"88pm860x-regulator", 10,},
124	{"88pm860x-regulator", 11,},
125	{"88pm860x-regulator", 12,},
126	{"88pm860x-regulator", 13,},
127	{"88pm860x-regulator", 14,},
128	{"88pm860x-regulator", 15,},
129	{"88pm860x-regulator", 16,},
130	{"88pm860x-regulator", 17,},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131};
132
133static struct mfd_cell touch_devs[] = {
134	{"88pm860x-touch", -1,},
135};
136
137static struct mfd_cell onkey_devs[] = {
138	{"88pm860x-onkey", -1,},
139};
140
141static struct mfd_cell codec_devs[] = {
142	{"88pm860x-codec", -1,},
143};
144
 
 
 
 
 
 
 
 
 
 
 
 
 
145static struct mfd_cell power_devs[] = {
146	{"88pm860x-battery", -1,},
147	{"88pm860x-charger", -1,},
 
 
148};
149
150static struct mfd_cell rtc_devs[] = {
151	{"88pm860x-rtc", -1,},
152};
153
154
155struct pm860x_irq_data {
156	int	reg;
157	int	mask_reg;
158	int	enable;		/* enable or not */
159	int	offs;		/* bit offset in mask register */
160};
161
162static struct pm860x_irq_data pm860x_irqs[] = {
163	[PM8607_IRQ_ONKEY] = {
164		.reg		= PM8607_INT_STATUS1,
165		.mask_reg	= PM8607_INT_MASK_1,
166		.offs		= 1 << 0,
167	},
168	[PM8607_IRQ_EXTON] = {
169		.reg		= PM8607_INT_STATUS1,
170		.mask_reg	= PM8607_INT_MASK_1,
171		.offs		= 1 << 1,
172	},
173	[PM8607_IRQ_CHG] = {
174		.reg		= PM8607_INT_STATUS1,
175		.mask_reg	= PM8607_INT_MASK_1,
176		.offs		= 1 << 2,
177	},
178	[PM8607_IRQ_BAT] = {
179		.reg		= PM8607_INT_STATUS1,
180		.mask_reg	= PM8607_INT_MASK_1,
181		.offs		= 1 << 3,
182	},
183	[PM8607_IRQ_RTC] = {
184		.reg		= PM8607_INT_STATUS1,
185		.mask_reg	= PM8607_INT_MASK_1,
186		.offs		= 1 << 4,
187	},
188	[PM8607_IRQ_CC] = {
189		.reg		= PM8607_INT_STATUS1,
190		.mask_reg	= PM8607_INT_MASK_1,
191		.offs		= 1 << 5,
192	},
193	[PM8607_IRQ_VBAT] = {
194		.reg		= PM8607_INT_STATUS2,
195		.mask_reg	= PM8607_INT_MASK_2,
196		.offs		= 1 << 0,
197	},
198	[PM8607_IRQ_VCHG] = {
199		.reg		= PM8607_INT_STATUS2,
200		.mask_reg	= PM8607_INT_MASK_2,
201		.offs		= 1 << 1,
202	},
203	[PM8607_IRQ_VSYS] = {
204		.reg		= PM8607_INT_STATUS2,
205		.mask_reg	= PM8607_INT_MASK_2,
206		.offs		= 1 << 2,
207	},
208	[PM8607_IRQ_TINT] = {
209		.reg		= PM8607_INT_STATUS2,
210		.mask_reg	= PM8607_INT_MASK_2,
211		.offs		= 1 << 3,
212	},
213	[PM8607_IRQ_GPADC0] = {
214		.reg		= PM8607_INT_STATUS2,
215		.mask_reg	= PM8607_INT_MASK_2,
216		.offs		= 1 << 4,
217	},
218	[PM8607_IRQ_GPADC1] = {
219		.reg		= PM8607_INT_STATUS2,
220		.mask_reg	= PM8607_INT_MASK_2,
221		.offs		= 1 << 5,
222	},
223	[PM8607_IRQ_GPADC2] = {
224		.reg		= PM8607_INT_STATUS2,
225		.mask_reg	= PM8607_INT_MASK_2,
226		.offs		= 1 << 6,
227	},
228	[PM8607_IRQ_GPADC3] = {
229		.reg		= PM8607_INT_STATUS2,
230		.mask_reg	= PM8607_INT_MASK_2,
231		.offs		= 1 << 7,
232	},
233	[PM8607_IRQ_AUDIO_SHORT] = {
234		.reg		= PM8607_INT_STATUS3,
235		.mask_reg	= PM8607_INT_MASK_3,
236		.offs		= 1 << 0,
237	},
238	[PM8607_IRQ_PEN] = {
239		.reg		= PM8607_INT_STATUS3,
240		.mask_reg	= PM8607_INT_MASK_3,
241		.offs		= 1 << 1,
242	},
243	[PM8607_IRQ_HEADSET] = {
244		.reg		= PM8607_INT_STATUS3,
245		.mask_reg	= PM8607_INT_MASK_3,
246		.offs		= 1 << 2,
247	},
248	[PM8607_IRQ_HOOK] = {
249		.reg		= PM8607_INT_STATUS3,
250		.mask_reg	= PM8607_INT_MASK_3,
251		.offs		= 1 << 3,
252	},
253	[PM8607_IRQ_MICIN] = {
254		.reg		= PM8607_INT_STATUS3,
255		.mask_reg	= PM8607_INT_MASK_3,
256		.offs		= 1 << 4,
257	},
258	[PM8607_IRQ_CHG_FAIL] = {
259		.reg		= PM8607_INT_STATUS3,
260		.mask_reg	= PM8607_INT_MASK_3,
261		.offs		= 1 << 5,
262	},
263	[PM8607_IRQ_CHG_DONE] = {
264		.reg		= PM8607_INT_STATUS3,
265		.mask_reg	= PM8607_INT_MASK_3,
266		.offs		= 1 << 6,
267	},
268	[PM8607_IRQ_CHG_FAULT] = {
269		.reg		= PM8607_INT_STATUS3,
270		.mask_reg	= PM8607_INT_MASK_3,
271		.offs		= 1 << 7,
272	},
273};
274
275static irqreturn_t pm860x_irq(int irq, void *data)
276{
277	struct pm860x_chip *chip = data;
278	struct pm860x_irq_data *irq_data;
279	struct i2c_client *i2c;
280	int read_reg = -1, value = 0;
281	int i;
282
283	i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
284	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
285		irq_data = &pm860x_irqs[i];
286		if (read_reg != irq_data->reg) {
287			read_reg = irq_data->reg;
288			value = pm860x_reg_read(i2c, irq_data->reg);
289		}
290		if (value & irq_data->enable)
291			handle_nested_irq(chip->irq_base + i);
292	}
293	return IRQ_HANDLED;
294}
295
296static void pm860x_irq_lock(struct irq_data *data)
297{
298	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
299
300	mutex_lock(&chip->irq_lock);
301}
302
303static void pm860x_irq_sync_unlock(struct irq_data *data)
304{
305	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
306	struct pm860x_irq_data *irq_data;
307	struct i2c_client *i2c;
308	static unsigned char cached[3] = {0x0, 0x0, 0x0};
309	unsigned char mask[3];
310	int i;
311
312	i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
313	/* Load cached value. In initial, all IRQs are masked */
314	for (i = 0; i < 3; i++)
315		mask[i] = cached[i];
316	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
317		irq_data = &pm860x_irqs[i];
318		switch (irq_data->mask_reg) {
319		case PM8607_INT_MASK_1:
320			mask[0] &= ~irq_data->offs;
321			mask[0] |= irq_data->enable;
322			break;
323		case PM8607_INT_MASK_2:
324			mask[1] &= ~irq_data->offs;
325			mask[1] |= irq_data->enable;
326			break;
327		case PM8607_INT_MASK_3:
328			mask[2] &= ~irq_data->offs;
329			mask[2] |= irq_data->enable;
330			break;
331		default:
332			dev_err(chip->dev, "wrong IRQ\n");
333			break;
334		}
335	}
336	/* update mask into registers */
337	for (i = 0; i < 3; i++) {
338		if (mask[i] != cached[i]) {
339			cached[i] = mask[i];
340			pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
341		}
342	}
343
344	mutex_unlock(&chip->irq_lock);
345}
346
347static void pm860x_irq_enable(struct irq_data *data)
348{
349	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
350	pm860x_irqs[data->irq - chip->irq_base].enable
351		= pm860x_irqs[data->irq - chip->irq_base].offs;
352}
353
354static void pm860x_irq_disable(struct irq_data *data)
355{
356	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
357	pm860x_irqs[data->irq - chip->irq_base].enable = 0;
358}
359
360static struct irq_chip pm860x_irq_chip = {
361	.name		= "88pm860x",
362	.irq_bus_lock	= pm860x_irq_lock,
363	.irq_bus_sync_unlock = pm860x_irq_sync_unlock,
364	.irq_enable	= pm860x_irq_enable,
365	.irq_disable	= pm860x_irq_disable,
366};
367
368static int __devinit device_gpadc_init(struct pm860x_chip *chip,
369				       struct pm860x_platform_data *pdata)
370{
371	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
372				: chip->companion;
373	int data;
374	int ret;
375
376	/* initialize GPADC without activating it */
377
378	if (!pdata || !pdata->touch)
379		return -EINVAL;
380
381	/* set GPADC MISC1 register */
382	data = 0;
383	data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
384	data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
385	data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
386	data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
387	if (data) {
388		ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
389		if (ret < 0)
390			goto out;
391	}
392	/* set tsi prebias time */
393	if (pdata->touch->tsi_prebias) {
394		data = pdata->touch->tsi_prebias;
395		ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
396		if (ret < 0)
397			goto out;
398	}
399	/* set prebias & prechg time of pen detect */
400	data = 0;
401	data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
402	data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
403	if (data) {
404		ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
405		if (ret < 0)
406			goto out;
407	}
408
409	ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
410			      PM8607_GPADC_EN, PM8607_GPADC_EN);
411out:
412	return ret;
413}
414
415static int __devinit device_irq_init(struct pm860x_chip *chip,
 
 
 
 
 
416				     struct pm860x_platform_data *pdata)
417{
418	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
419				: chip->companion;
420	unsigned char status_buf[INT_STATUS_NUM];
421	unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
422	int i, data, mask, ret = -EINVAL;
423	int __irq;
424
425	if (!pdata || !pdata->irq_base) {
426		dev_warn(chip->dev, "No interrupt support on IRQ base\n");
427		return -EINVAL;
428	}
429
430	mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
431		| PM8607_B0_MISC1_INT_MASK;
432	data = 0;
433	chip->irq_mode = 0;
434	if (pdata && pdata->irq_mode) {
435		/*
436		 * irq_mode defines the way of clearing interrupt. If it's 1,
437		 * clear IRQ by write. Otherwise, clear it by read.
438		 * This control bit is valid from 88PM8607 B0 steping.
439		 */
440		data |= PM8607_B0_MISC1_INT_CLEAR;
441		chip->irq_mode = 1;
442	}
443	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
444	if (ret < 0)
445		goto out;
446
447	/* mask all IRQs */
448	memset(status_buf, 0, INT_STATUS_NUM);
449	ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
450				INT_STATUS_NUM, status_buf);
451	if (ret < 0)
452		goto out;
453
454	if (chip->irq_mode) {
455		/* clear interrupt status by write */
456		memset(status_buf, 0xFF, INT_STATUS_NUM);
457		ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
458					INT_STATUS_NUM, status_buf);
459	} else {
460		/* clear interrupt status by read */
461		ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
462					INT_STATUS_NUM, status_buf);
463	}
464	if (ret < 0)
465		goto out;
466
467	mutex_init(&chip->irq_lock);
468	chip->irq_base = pdata->irq_base;
 
 
 
 
 
 
 
 
 
 
 
 
469	chip->core_irq = i2c->irq;
470	if (!chip->core_irq)
471		goto out;
472
473	/* register IRQ by genirq */
474	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
475		__irq = i + chip->irq_base;
476		irq_set_chip_data(__irq, chip);
477		irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
478					 handle_edge_irq);
479		irq_set_nested_thread(__irq, 1);
480#ifdef CONFIG_ARM
481		set_irq_flags(__irq, IRQF_VALID);
482#else
483		irq_set_noprobe(__irq);
484#endif
485	}
486
487	ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
488				   "88pm860x", chip);
489	if (ret) {
490		dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
491		chip->core_irq = 0;
492	}
493
494	return 0;
495out:
496	chip->core_irq = 0;
497	return ret;
498}
499
500static void device_irq_exit(struct pm860x_chip *chip)
501{
502	if (chip->core_irq)
503		free_irq(chip->core_irq, chip);
504}
505
506static void __devinit device_bk_init(struct pm860x_chip *chip,
507				     struct pm860x_platform_data *pdata)
508{
509	int ret;
510	int i, j, id;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511
512	if ((pdata == NULL) || (pdata->backlight == NULL))
513		return;
 
 
 
 
 
 
514
515	if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
516		pdata->num_backlights = ARRAY_SIZE(bk_devs);
 
 
 
 
 
 
 
517
518	for (i = 0; i < pdata->num_backlights; i++) {
519		bk_devs[i].platform_data = &pdata->backlight[i];
520		bk_devs[i].pdata_size = sizeof(struct pm860x_backlight_pdata);
521
522		for (j = 0; j < ARRAY_SIZE(bk_devs); j++) {
523			id = bk_resources[j].start;
524			if (pdata->backlight[i].flags != id)
525				continue;
526
527			bk_devs[i].num_resources = 1;
528			bk_devs[i].resources = &bk_resources[j];
529			ret = mfd_add_devices(chip->dev, 0,
530					      &bk_devs[i], 1,
531					      &bk_resources[j], 0);
532			if (ret < 0) {
533				dev_err(chip->dev, "Failed to add "
534					"backlight subdev\n");
535				return;
536			}
537		}
 
 
 
 
 
 
 
 
538	}
 
 
 
 
 
 
 
 
 
539}
 
540
541static void __devinit device_led_init(struct pm860x_chip *chip,
542				      struct pm860x_platform_data *pdata)
543{
544	int ret;
545	int i, j, id;
546
547	if ((pdata == NULL) || (pdata->led == NULL))
548		return;
 
 
 
 
 
 
 
 
549
550	if (pdata->num_leds > ARRAY_SIZE(led_devs))
551		pdata->num_leds = ARRAY_SIZE(led_devs);
 
 
552
553	for (i = 0; i < pdata->num_leds; i++) {
554		led_devs[i].platform_data = &pdata->led[i];
555		led_devs[i].pdata_size = sizeof(struct pm860x_led_pdata);
556
557		for (j = 0; j < ARRAY_SIZE(led_devs); j++) {
558			id = led_resources[j].start;
559			if (pdata->led[i].flags != id)
560				continue;
561
562			led_devs[i].num_resources = 1;
563			led_devs[i].resources = &led_resources[j],
564			ret = mfd_add_devices(chip->dev, 0,
565					      &led_devs[i], 1,
566					      &led_resources[j], 0);
567			if (ret < 0) {
568				dev_err(chip->dev, "Failed to add "
569					"led subdev\n");
570				return;
571			}
572		}
573	}
 
 
 
 
574}
575
576static void __devinit device_regulator_init(struct pm860x_chip *chip,
577					    struct pm860x_platform_data *pdata)
578{
579	struct regulator_init_data *initdata;
580	int ret;
581	int i, seq;
582
583	if ((pdata == NULL) || (pdata->regulator == NULL))
 
 
 
 
 
 
 
 
 
 
 
 
584		return;
 
 
585
586	if (pdata->num_regulators > ARRAY_SIZE(regulator_devs))
587		pdata->num_regulators = ARRAY_SIZE(regulator_devs);
 
 
588
589	for (i = 0, seq = -1; i < pdata->num_regulators; i++) {
590		initdata = &pdata->regulator[i];
591		seq = *(unsigned int *)initdata->driver_data;
592		if ((seq < 0) || (seq > PM8607_ID_RG_MAX)) {
593			dev_err(chip->dev, "Wrong ID(%d) on regulator(%s)\n",
594				seq, initdata->constraints.name);
595			goto out;
596		}
597		regulator_devs[i].platform_data = &pdata->regulator[i];
598		regulator_devs[i].pdata_size = sizeof(struct regulator_init_data);
599		regulator_devs[i].num_resources = 1;
600		regulator_devs[i].resources = &regulator_resources[seq];
601
602		ret = mfd_add_devices(chip->dev, 0, &regulator_devs[i], 1,
603				      &regulator_resources[seq], 0);
604		if (ret < 0) {
605			dev_err(chip->dev, "Failed to add regulator subdev\n");
606			goto out;
607		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608	}
609out:
610	return;
611}
612
613static void __devinit device_rtc_init(struct pm860x_chip *chip,
614				      struct pm860x_platform_data *pdata)
615{
616	int ret;
617
618	if ((pdata == NULL))
619		return;
620
621	rtc_devs[0].platform_data = pdata->rtc;
622	rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
623	rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
624	rtc_devs[0].resources = &rtc_resources[0];
625	ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
626			      ARRAY_SIZE(rtc_devs), &rtc_resources[0],
627			      chip->irq_base);
628	if (ret < 0)
629		dev_err(chip->dev, "Failed to add rtc subdev\n");
630}
631
632static void __devinit device_touch_init(struct pm860x_chip *chip,
633					struct pm860x_platform_data *pdata)
634{
635	int ret;
636
637	if (pdata == NULL)
638		return;
639
640	touch_devs[0].platform_data = pdata->touch;
641	touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
642	touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
643	touch_devs[0].resources = &touch_resources[0];
644	ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
645			      ARRAY_SIZE(touch_devs), &touch_resources[0],
646			      chip->irq_base);
647	if (ret < 0)
648		dev_err(chip->dev, "Failed to add touch subdev\n");
649}
650
651static void __devinit device_power_init(struct pm860x_chip *chip,
652					struct pm860x_platform_data *pdata)
653{
654	int ret;
655
656	if (pdata == NULL)
657		return;
658
659	power_devs[0].platform_data = pdata->power;
660	power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
661	power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
662	power_devs[0].resources = &battery_resources[0],
663	ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
664			      &battery_resources[0], chip->irq_base);
665	if (ret < 0)
666		dev_err(chip->dev, "Failed to add battery subdev\n");
667
668	power_devs[1].platform_data = pdata->power;
669	power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
670	power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
671	power_devs[1].resources = &charger_resources[0],
672	ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
673			      &charger_resources[0], chip->irq_base);
674	if (ret < 0)
675		dev_err(chip->dev, "Failed to add charger subdev\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
676}
677
678static void __devinit device_onkey_init(struct pm860x_chip *chip,
679					struct pm860x_platform_data *pdata)
680{
681	int ret;
682
683	onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
684	onkey_devs[0].resources = &onkey_resources[0],
685	ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
686			      ARRAY_SIZE(onkey_devs), &onkey_resources[0],
687			      chip->irq_base);
688	if (ret < 0)
689		dev_err(chip->dev, "Failed to add onkey subdev\n");
690}
691
692static void __devinit device_codec_init(struct pm860x_chip *chip,
693					struct pm860x_platform_data *pdata)
694{
695	int ret;
696
697	codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
698	codec_devs[0].resources = &codec_resources[0],
699	ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
700			      ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
 
701	if (ret < 0)
702		dev_err(chip->dev, "Failed to add codec subdev\n");
703}
704
705static void __devinit device_8607_init(struct pm860x_chip *chip,
706				       struct i2c_client *i2c,
707				       struct pm860x_platform_data *pdata)
708{
709	int data, ret;
710
711	ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
712	if (ret < 0) {
713		dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
714		goto out;
715	}
716	switch (ret & PM8607_VERSION_MASK) {
717	case 0x40:
718	case 0x50:
719		dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
720			 ret);
721		break;
722	default:
723		dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
724			"Chip ID: %02x\n", ret);
 
725		goto out;
726	}
727
728	ret = pm860x_reg_read(i2c, PM8607_BUCK3);
729	if (ret < 0) {
730		dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
731		goto out;
732	}
733	if (ret & PM8607_BUCK3_DOUBLE)
734		chip->buck3_double = 1;
735
736	ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
737	if (ret < 0) {
738		dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
739		goto out;
740	}
741
742	if (pdata && (pdata->i2c_port == PI2C_PORT))
743		data = PM8607_B0_MISC1_PI2C;
744	else
745		data = 0;
746	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
747	if (ret < 0) {
748		dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
749		goto out;
750	}
751
752	ret = device_gpadc_init(chip, pdata);
753	if (ret < 0)
754		goto out;
755
756	ret = device_irq_init(chip, pdata);
757	if (ret < 0)
758		goto out;
759
760	device_regulator_init(chip, pdata);
761	device_rtc_init(chip, pdata);
762	device_onkey_init(chip, pdata);
763	device_touch_init(chip, pdata);
764	device_power_init(chip, pdata);
765	device_codec_init(chip, pdata);
766out:
767	return;
768}
769
770int __devinit pm860x_device_init(struct pm860x_chip *chip,
771		       struct pm860x_platform_data *pdata)
 
 
 
 
 
 
 
 
 
772{
773	chip->core_irq = 0;
774
775	switch (chip->id) {
776	case CHIP_PM8606:
777		device_bk_init(chip, pdata);
778		device_led_init(chip, pdata);
779		break;
780	case CHIP_PM8607:
781		device_8607_init(chip, chip->client, pdata);
782		break;
783	}
784
785	if (chip->companion) {
786		switch (chip->id) {
787		case CHIP_PM8607:
788			device_bk_init(chip, pdata);
789			device_led_init(chip, pdata);
790			break;
791		case CHIP_PM8606:
792			device_8607_init(chip, chip->companion, pdata);
793			break;
794		}
795	}
796
797	return 0;
798}
799
800void __devexit pm860x_device_exit(struct pm860x_chip *chip)
801{
802	device_irq_exit(chip);
803	mfd_remove_devices(chip->dev);
804}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805
806MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
807MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
808MODULE_LICENSE("GPL");
v4.10.11
   1/*
   2 * Base driver for Marvell 88PM8607
   3 *
   4 * Copyright (C) 2009 Marvell International Ltd.
   5 *
   6 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/err.h>
  16#include <linux/i2c.h>
  17#include <linux/irq.h>
  18#include <linux/interrupt.h>
  19#include <linux/irqdomain.h>
  20#include <linux/of.h>
  21#include <linux/of_platform.h>
  22#include <linux/platform_device.h>
  23#include <linux/regmap.h>
  24#include <linux/slab.h>
  25#include <linux/mfd/core.h>
  26#include <linux/mfd/88pm860x.h>
  27#include <linux/regulator/machine.h>
  28#include <linux/power/charger-manager.h>
  29
  30#define INT_STATUS_NUM			3
  31
  32static struct resource bk0_resources[] = {
  33	{2, 2, "duty cycle", IORESOURCE_REG, },
  34	{3, 3, "always on",  IORESOURCE_REG, },
  35	{3, 3, "current",    IORESOURCE_REG, },
  36};
  37static struct resource bk1_resources[] = {
  38	{4, 4, "duty cycle", IORESOURCE_REG, },
  39	{5, 5, "always on",  IORESOURCE_REG, },
  40	{5, 5, "current",    IORESOURCE_REG, },
  41};
  42static struct resource bk2_resources[] = {
  43	{6, 6, "duty cycle", IORESOURCE_REG, },
  44	{7, 7, "always on",  IORESOURCE_REG, },
  45	{5, 5, "current",    IORESOURCE_REG, },
  46};
  47
  48static struct resource led0_resources[] = {
  49	/* RGB1 Red LED */
  50	{0xd, 0xd, "control", IORESOURCE_REG, },
  51	{0xc, 0xc, "blink",   IORESOURCE_REG, },
  52};
  53static struct resource led1_resources[] = {
  54	/* RGB1 Green LED */
  55	{0xe, 0xe, "control", IORESOURCE_REG, },
  56	{0xc, 0xc, "blink",   IORESOURCE_REG, },
  57};
  58static struct resource led2_resources[] = {
  59	/* RGB1 Blue LED */
  60	{0xf, 0xf, "control", IORESOURCE_REG, },
  61	{0xc, 0xc, "blink",   IORESOURCE_REG, },
  62};
  63static struct resource led3_resources[] = {
  64	/* RGB2 Red LED */
  65	{0x9, 0x9, "control", IORESOURCE_REG, },
  66	{0x8, 0x8, "blink",   IORESOURCE_REG, },
  67};
  68static struct resource led4_resources[] = {
  69	/* RGB2 Green LED */
  70	{0xa, 0xa, "control", IORESOURCE_REG, },
  71	{0x8, 0x8, "blink",   IORESOURCE_REG, },
  72};
  73static struct resource led5_resources[] = {
  74	/* RGB2 Blue LED */
  75	{0xb, 0xb, "control", IORESOURCE_REG, },
  76	{0x8, 0x8, "blink",   IORESOURCE_REG, },
  77};
  78
  79static struct resource buck1_resources[] = {
  80	{0x24, 0x24, "buck set", IORESOURCE_REG, },
  81};
  82static struct resource buck2_resources[] = {
  83	{0x25, 0x25, "buck set", IORESOURCE_REG, },
  84};
  85static struct resource buck3_resources[] = {
  86	{0x26, 0x26, "buck set", IORESOURCE_REG, },
  87};
  88static struct resource ldo1_resources[] = {
  89	{0x10, 0x10, "ldo set", IORESOURCE_REG, },
  90};
  91static struct resource ldo2_resources[] = {
  92	{0x11, 0x11, "ldo set", IORESOURCE_REG, },
  93};
  94static struct resource ldo3_resources[] = {
  95	{0x12, 0x12, "ldo set", IORESOURCE_REG, },
  96};
  97static struct resource ldo4_resources[] = {
  98	{0x13, 0x13, "ldo set", IORESOURCE_REG, },
  99};
 100static struct resource ldo5_resources[] = {
 101	{0x14, 0x14, "ldo set", IORESOURCE_REG, },
 102};
 103static struct resource ldo6_resources[] = {
 104	{0x15, 0x15, "ldo set", IORESOURCE_REG, },
 105};
 106static struct resource ldo7_resources[] = {
 107	{0x16, 0x16, "ldo set", IORESOURCE_REG, },
 108};
 109static struct resource ldo8_resources[] = {
 110	{0x17, 0x17, "ldo set", IORESOURCE_REG, },
 111};
 112static struct resource ldo9_resources[] = {
 113	{0x18, 0x18, "ldo set", IORESOURCE_REG, },
 114};
 115static struct resource ldo10_resources[] = {
 116	{0x19, 0x19, "ldo set", IORESOURCE_REG, },
 117};
 118static struct resource ldo12_resources[] = {
 119	{0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
 120};
 121static struct resource ldo_vibrator_resources[] = {
 122	{0x28, 0x28, "ldo set", IORESOURCE_REG, },
 123};
 124static struct resource ldo14_resources[] = {
 125	{0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
 126};
 127
 128static struct resource touch_resources[] = {
 129	{PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
 130};
 131
 132static struct resource onkey_resources[] = {
 133	{PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
 134};
 135
 136static struct resource codec_resources[] = {
 137	/* Headset microphone insertion or removal */
 138	{PM8607_IRQ_MICIN,   PM8607_IRQ_MICIN,   "micin",   IORESOURCE_IRQ,},
 139	/* Hook-switch press or release */
 140	{PM8607_IRQ_HOOK,    PM8607_IRQ_HOOK,    "hook",    IORESOURCE_IRQ,},
 141	/* Headset insertion or removal */
 142	{PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
 143	/* Audio short */
 144	{PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short",
 145	 IORESOURCE_IRQ,},
 146};
 147
 148static struct resource battery_resources[] = {
 149	{PM8607_IRQ_CC,  PM8607_IRQ_CC,  "columb counter", IORESOURCE_IRQ,},
 150	{PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery",        IORESOURCE_IRQ,},
 151};
 152
 153static struct resource charger_resources[] = {
 154	{PM8607_IRQ_CHG,  PM8607_IRQ_CHG,  "charger detect",  IORESOURCE_IRQ,},
 155	{PM8607_IRQ_CHG_DONE,  PM8607_IRQ_CHG_DONE,  "charging done",
 156	 IORESOURCE_IRQ,},
 157	{PM8607_IRQ_CHG_FAIL,  PM8607_IRQ_CHG_FAIL,  "charging timeout",
 158	 IORESOURCE_IRQ,},
 159	{PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault",
 160	 IORESOURCE_IRQ,},
 161	{PM8607_IRQ_GPADC1,    PM8607_IRQ_GPADC1,    "battery temperature",
 162	 IORESOURCE_IRQ,},
 163	{PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
 164	{PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage",    IORESOURCE_IRQ,},
 165};
 166
 167static struct resource rtc_resources[] = {
 168	{PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
 169};
 170
 171static struct mfd_cell bk_devs[] = {
 172	{
 173		.name = "88pm860x-backlight",
 174		.id = 0,
 175		.num_resources = ARRAY_SIZE(bk0_resources),
 176		.resources = bk0_resources,
 177	}, {
 178		.name = "88pm860x-backlight",
 179		.id = 1,
 180		.num_resources = ARRAY_SIZE(bk1_resources),
 181		.resources = bk1_resources,
 182	}, {
 183		.name = "88pm860x-backlight",
 184		.id = 2,
 185		.num_resources = ARRAY_SIZE(bk2_resources),
 186		.resources = bk2_resources,
 187	},
 188};
 189
 190static struct mfd_cell led_devs[] = {
 191	{
 192		.name = "88pm860x-led",
 193		.id = 0,
 194		.num_resources = ARRAY_SIZE(led0_resources),
 195		.resources = led0_resources,
 196	}, {
 197		.name = "88pm860x-led",
 198		.id = 1,
 199		.num_resources = ARRAY_SIZE(led1_resources),
 200		.resources = led1_resources,
 201	}, {
 202		.name = "88pm860x-led",
 203		.id = 2,
 204		.num_resources = ARRAY_SIZE(led2_resources),
 205		.resources = led2_resources,
 206	}, {
 207		.name = "88pm860x-led",
 208		.id = 3,
 209		.num_resources = ARRAY_SIZE(led3_resources),
 210		.resources = led3_resources,
 211	}, {
 212		.name = "88pm860x-led",
 213		.id = 4,
 214		.num_resources = ARRAY_SIZE(led4_resources),
 215		.resources = led4_resources,
 216	}, {
 217		.name = "88pm860x-led",
 218		.id = 5,
 219		.num_resources = ARRAY_SIZE(led5_resources),
 220		.resources = led5_resources,
 221	},
 222};
 223
 224static struct mfd_cell reg_devs[] = {
 225	{
 226		.name = "88pm860x-regulator",
 227		.id = 0,
 228		.num_resources = ARRAY_SIZE(buck1_resources),
 229		.resources = buck1_resources,
 230	}, {
 231		.name = "88pm860x-regulator",
 232		.id = 1,
 233		.num_resources = ARRAY_SIZE(buck2_resources),
 234		.resources = buck2_resources,
 235	}, {
 236		.name = "88pm860x-regulator",
 237		.id = 2,
 238		.num_resources = ARRAY_SIZE(buck3_resources),
 239		.resources = buck3_resources,
 240	}, {
 241		.name = "88pm860x-regulator",
 242		.id = 3,
 243		.num_resources = ARRAY_SIZE(ldo1_resources),
 244		.resources = ldo1_resources,
 245	}, {
 246		.name = "88pm860x-regulator",
 247		.id = 4,
 248		.num_resources = ARRAY_SIZE(ldo2_resources),
 249		.resources = ldo2_resources,
 250	}, {
 251		.name = "88pm860x-regulator",
 252		.id = 5,
 253		.num_resources = ARRAY_SIZE(ldo3_resources),
 254		.resources = ldo3_resources,
 255	}, {
 256		.name = "88pm860x-regulator",
 257		.id = 6,
 258		.num_resources = ARRAY_SIZE(ldo4_resources),
 259		.resources = ldo4_resources,
 260	}, {
 261		.name = "88pm860x-regulator",
 262		.id = 7,
 263		.num_resources = ARRAY_SIZE(ldo5_resources),
 264		.resources = ldo5_resources,
 265	}, {
 266		.name = "88pm860x-regulator",
 267		.id = 8,
 268		.num_resources = ARRAY_SIZE(ldo6_resources),
 269		.resources = ldo6_resources,
 270	}, {
 271		.name = "88pm860x-regulator",
 272		.id = 9,
 273		.num_resources = ARRAY_SIZE(ldo7_resources),
 274		.resources = ldo7_resources,
 275	}, {
 276		.name = "88pm860x-regulator",
 277		.id = 10,
 278		.num_resources = ARRAY_SIZE(ldo8_resources),
 279		.resources = ldo8_resources,
 280	}, {
 281		.name = "88pm860x-regulator",
 282		.id = 11,
 283		.num_resources = ARRAY_SIZE(ldo9_resources),
 284		.resources = ldo9_resources,
 285	}, {
 286		.name = "88pm860x-regulator",
 287		.id = 12,
 288		.num_resources = ARRAY_SIZE(ldo10_resources),
 289		.resources = ldo10_resources,
 290	}, {
 291		.name = "88pm860x-regulator",
 292		.id = 13,
 293		.num_resources = ARRAY_SIZE(ldo12_resources),
 294		.resources = ldo12_resources,
 295	}, {
 296		.name = "88pm860x-regulator",
 297		.id = 14,
 298		.num_resources = ARRAY_SIZE(ldo_vibrator_resources),
 299		.resources = ldo_vibrator_resources,
 300	}, {
 301		.name = "88pm860x-regulator",
 302		.id = 15,
 303		.num_resources = ARRAY_SIZE(ldo14_resources),
 304		.resources = ldo14_resources,
 305	},
 306};
 307
 308static struct mfd_cell touch_devs[] = {
 309	{"88pm860x-touch", -1,},
 310};
 311
 312static struct mfd_cell onkey_devs[] = {
 313	{"88pm860x-onkey", -1,},
 314};
 315
 316static struct mfd_cell codec_devs[] = {
 317	{"88pm860x-codec", -1,},
 318};
 319
 320static struct regulator_consumer_supply preg_supply[] = {
 321	REGULATOR_SUPPLY("preg", "charger-manager"),
 322};
 323
 324static struct regulator_init_data preg_init_data = {
 325	.num_consumer_supplies	= ARRAY_SIZE(preg_supply),
 326	.consumer_supplies	= &preg_supply[0],
 327};
 328
 329static struct charger_regulator chg_desc_regulator_data[] = {
 330	{ .regulator_name = "preg", },
 331};
 332
 333static struct mfd_cell power_devs[] = {
 334	{"88pm860x-battery", -1,},
 335	{"88pm860x-charger", -1,},
 336	{"88pm860x-preg",    -1,},
 337	{"charger-manager", -1,},
 338};
 339
 340static struct mfd_cell rtc_devs[] = {
 341	{"88pm860x-rtc", -1,},
 342};
 343
 344
 345struct pm860x_irq_data {
 346	int	reg;
 347	int	mask_reg;
 348	int	enable;		/* enable or not */
 349	int	offs;		/* bit offset in mask register */
 350};
 351
 352static struct pm860x_irq_data pm860x_irqs[] = {
 353	[PM8607_IRQ_ONKEY] = {
 354		.reg		= PM8607_INT_STATUS1,
 355		.mask_reg	= PM8607_INT_MASK_1,
 356		.offs		= 1 << 0,
 357	},
 358	[PM8607_IRQ_EXTON] = {
 359		.reg		= PM8607_INT_STATUS1,
 360		.mask_reg	= PM8607_INT_MASK_1,
 361		.offs		= 1 << 1,
 362	},
 363	[PM8607_IRQ_CHG] = {
 364		.reg		= PM8607_INT_STATUS1,
 365		.mask_reg	= PM8607_INT_MASK_1,
 366		.offs		= 1 << 2,
 367	},
 368	[PM8607_IRQ_BAT] = {
 369		.reg		= PM8607_INT_STATUS1,
 370		.mask_reg	= PM8607_INT_MASK_1,
 371		.offs		= 1 << 3,
 372	},
 373	[PM8607_IRQ_RTC] = {
 374		.reg		= PM8607_INT_STATUS1,
 375		.mask_reg	= PM8607_INT_MASK_1,
 376		.offs		= 1 << 4,
 377	},
 378	[PM8607_IRQ_CC] = {
 379		.reg		= PM8607_INT_STATUS1,
 380		.mask_reg	= PM8607_INT_MASK_1,
 381		.offs		= 1 << 5,
 382	},
 383	[PM8607_IRQ_VBAT] = {
 384		.reg		= PM8607_INT_STATUS2,
 385		.mask_reg	= PM8607_INT_MASK_2,
 386		.offs		= 1 << 0,
 387	},
 388	[PM8607_IRQ_VCHG] = {
 389		.reg		= PM8607_INT_STATUS2,
 390		.mask_reg	= PM8607_INT_MASK_2,
 391		.offs		= 1 << 1,
 392	},
 393	[PM8607_IRQ_VSYS] = {
 394		.reg		= PM8607_INT_STATUS2,
 395		.mask_reg	= PM8607_INT_MASK_2,
 396		.offs		= 1 << 2,
 397	},
 398	[PM8607_IRQ_TINT] = {
 399		.reg		= PM8607_INT_STATUS2,
 400		.mask_reg	= PM8607_INT_MASK_2,
 401		.offs		= 1 << 3,
 402	},
 403	[PM8607_IRQ_GPADC0] = {
 404		.reg		= PM8607_INT_STATUS2,
 405		.mask_reg	= PM8607_INT_MASK_2,
 406		.offs		= 1 << 4,
 407	},
 408	[PM8607_IRQ_GPADC1] = {
 409		.reg		= PM8607_INT_STATUS2,
 410		.mask_reg	= PM8607_INT_MASK_2,
 411		.offs		= 1 << 5,
 412	},
 413	[PM8607_IRQ_GPADC2] = {
 414		.reg		= PM8607_INT_STATUS2,
 415		.mask_reg	= PM8607_INT_MASK_2,
 416		.offs		= 1 << 6,
 417	},
 418	[PM8607_IRQ_GPADC3] = {
 419		.reg		= PM8607_INT_STATUS2,
 420		.mask_reg	= PM8607_INT_MASK_2,
 421		.offs		= 1 << 7,
 422	},
 423	[PM8607_IRQ_AUDIO_SHORT] = {
 424		.reg		= PM8607_INT_STATUS3,
 425		.mask_reg	= PM8607_INT_MASK_3,
 426		.offs		= 1 << 0,
 427	},
 428	[PM8607_IRQ_PEN] = {
 429		.reg		= PM8607_INT_STATUS3,
 430		.mask_reg	= PM8607_INT_MASK_3,
 431		.offs		= 1 << 1,
 432	},
 433	[PM8607_IRQ_HEADSET] = {
 434		.reg		= PM8607_INT_STATUS3,
 435		.mask_reg	= PM8607_INT_MASK_3,
 436		.offs		= 1 << 2,
 437	},
 438	[PM8607_IRQ_HOOK] = {
 439		.reg		= PM8607_INT_STATUS3,
 440		.mask_reg	= PM8607_INT_MASK_3,
 441		.offs		= 1 << 3,
 442	},
 443	[PM8607_IRQ_MICIN] = {
 444		.reg		= PM8607_INT_STATUS3,
 445		.mask_reg	= PM8607_INT_MASK_3,
 446		.offs		= 1 << 4,
 447	},
 448	[PM8607_IRQ_CHG_FAIL] = {
 449		.reg		= PM8607_INT_STATUS3,
 450		.mask_reg	= PM8607_INT_MASK_3,
 451		.offs		= 1 << 5,
 452	},
 453	[PM8607_IRQ_CHG_DONE] = {
 454		.reg		= PM8607_INT_STATUS3,
 455		.mask_reg	= PM8607_INT_MASK_3,
 456		.offs		= 1 << 6,
 457	},
 458	[PM8607_IRQ_CHG_FAULT] = {
 459		.reg		= PM8607_INT_STATUS3,
 460		.mask_reg	= PM8607_INT_MASK_3,
 461		.offs		= 1 << 7,
 462	},
 463};
 464
 465static irqreturn_t pm860x_irq(int irq, void *data)
 466{
 467	struct pm860x_chip *chip = data;
 468	struct pm860x_irq_data *irq_data;
 469	struct i2c_client *i2c;
 470	int read_reg = -1, value = 0;
 471	int i;
 472
 473	i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
 474	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
 475		irq_data = &pm860x_irqs[i];
 476		if (read_reg != irq_data->reg) {
 477			read_reg = irq_data->reg;
 478			value = pm860x_reg_read(i2c, irq_data->reg);
 479		}
 480		if (value & irq_data->enable)
 481			handle_nested_irq(chip->irq_base + i);
 482	}
 483	return IRQ_HANDLED;
 484}
 485
 486static void pm860x_irq_lock(struct irq_data *data)
 487{
 488	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
 489
 490	mutex_lock(&chip->irq_lock);
 491}
 492
 493static void pm860x_irq_sync_unlock(struct irq_data *data)
 494{
 495	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
 496	struct pm860x_irq_data *irq_data;
 497	struct i2c_client *i2c;
 498	static unsigned char cached[3] = {0x0, 0x0, 0x0};
 499	unsigned char mask[3];
 500	int i;
 501
 502	i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
 503	/* Load cached value. In initial, all IRQs are masked */
 504	for (i = 0; i < 3; i++)
 505		mask[i] = cached[i];
 506	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
 507		irq_data = &pm860x_irqs[i];
 508		switch (irq_data->mask_reg) {
 509		case PM8607_INT_MASK_1:
 510			mask[0] &= ~irq_data->offs;
 511			mask[0] |= irq_data->enable;
 512			break;
 513		case PM8607_INT_MASK_2:
 514			mask[1] &= ~irq_data->offs;
 515			mask[1] |= irq_data->enable;
 516			break;
 517		case PM8607_INT_MASK_3:
 518			mask[2] &= ~irq_data->offs;
 519			mask[2] |= irq_data->enable;
 520			break;
 521		default:
 522			dev_err(chip->dev, "wrong IRQ\n");
 523			break;
 524		}
 525	}
 526	/* update mask into registers */
 527	for (i = 0; i < 3; i++) {
 528		if (mask[i] != cached[i]) {
 529			cached[i] = mask[i];
 530			pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
 531		}
 532	}
 533
 534	mutex_unlock(&chip->irq_lock);
 535}
 536
 537static void pm860x_irq_enable(struct irq_data *data)
 538{
 539	pm860x_irqs[data->hwirq].enable = pm860x_irqs[data->hwirq].offs;
 
 
 540}
 541
 542static void pm860x_irq_disable(struct irq_data *data)
 543{
 544	pm860x_irqs[data->hwirq].enable = 0;
 
 545}
 546
 547static struct irq_chip pm860x_irq_chip = {
 548	.name		= "88pm860x",
 549	.irq_bus_lock	= pm860x_irq_lock,
 550	.irq_bus_sync_unlock = pm860x_irq_sync_unlock,
 551	.irq_enable	= pm860x_irq_enable,
 552	.irq_disable	= pm860x_irq_disable,
 553};
 554
 555static int pm860x_irq_domain_map(struct irq_domain *d, unsigned int virq,
 556				 irq_hw_number_t hw)
 557{
 558	irq_set_chip_data(virq, d->host_data);
 559	irq_set_chip_and_handler(virq, &pm860x_irq_chip, handle_edge_irq);
 560	irq_set_nested_thread(virq, 1);
 561	irq_set_noprobe(virq);
 562	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 563}
 564
 565static const struct irq_domain_ops pm860x_irq_domain_ops = {
 566	.map	= pm860x_irq_domain_map,
 567	.xlate	= irq_domain_xlate_onetwocell,
 568};
 569
 570static int device_irq_init(struct pm860x_chip *chip,
 571				     struct pm860x_platform_data *pdata)
 572{
 573	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ?
 574		chip->client : chip->companion;
 575	unsigned char status_buf[INT_STATUS_NUM];
 576	unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
 577	int data, mask, ret = -EINVAL;
 578	int nr_irqs, irq_base = -1;
 579	struct device_node *node = i2c->dev.of_node;
 
 
 
 
 580
 581	mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
 582		| PM8607_B0_MISC1_INT_MASK;
 583	data = 0;
 584	chip->irq_mode = 0;
 585	if (pdata && pdata->irq_mode) {
 586		/*
 587		 * irq_mode defines the way of clearing interrupt. If it's 1,
 588		 * clear IRQ by write. Otherwise, clear it by read.
 589		 * This control bit is valid from 88PM8607 B0 steping.
 590		 */
 591		data |= PM8607_B0_MISC1_INT_CLEAR;
 592		chip->irq_mode = 1;
 593	}
 594	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
 595	if (ret < 0)
 596		goto out;
 597
 598	/* mask all IRQs */
 599	memset(status_buf, 0, INT_STATUS_NUM);
 600	ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
 601				INT_STATUS_NUM, status_buf);
 602	if (ret < 0)
 603		goto out;
 604
 605	if (chip->irq_mode) {
 606		/* clear interrupt status by write */
 607		memset(status_buf, 0xFF, INT_STATUS_NUM);
 608		ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
 609					INT_STATUS_NUM, status_buf);
 610	} else {
 611		/* clear interrupt status by read */
 612		ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
 613					INT_STATUS_NUM, status_buf);
 614	}
 615	if (ret < 0)
 616		goto out;
 617
 618	mutex_init(&chip->irq_lock);
 619
 620	if (pdata && pdata->irq_base)
 621		irq_base = pdata->irq_base;
 622	nr_irqs = ARRAY_SIZE(pm860x_irqs);
 623	chip->irq_base = irq_alloc_descs(irq_base, 0, nr_irqs, 0);
 624	if (chip->irq_base < 0) {
 625		dev_err(&i2c->dev, "Failed to allocate interrupts, ret:%d\n",
 626			chip->irq_base);
 627		ret = -EBUSY;
 628		goto out;
 629	}
 630	irq_domain_add_legacy(node, nr_irqs, chip->irq_base, 0,
 631			      &pm860x_irq_domain_ops, chip);
 632	chip->core_irq = i2c->irq;
 633	if (!chip->core_irq)
 634		goto out;
 635
 636	ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq,
 637				   flags | IRQF_ONESHOT, "88pm860x", chip);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 638	if (ret) {
 639		dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
 640		chip->core_irq = 0;
 641	}
 642
 643	return 0;
 644out:
 645	chip->core_irq = 0;
 646	return ret;
 647}
 648
 649static void device_irq_exit(struct pm860x_chip *chip)
 650{
 651	if (chip->core_irq)
 652		free_irq(chip->core_irq, chip);
 653}
 654
 655int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
 
 656{
 657	int ret = -EIO;
 658	struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
 659		chip->client : chip->companion;
 660
 661	dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
 662	dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
 663			__func__, chip->osc_vote,
 664			chip->osc_status);
 665
 666	mutex_lock(&chip->osc_lock);
 667	/* Update voting status */
 668	chip->osc_vote |= client;
 669	/* If reference group is off - turn on*/
 670	if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
 671		chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
 672		/* Enable Reference group Vsys */
 673		if (pm860x_set_bits(i2c, PM8606_VSYS,
 674				PM8606_VSYS_EN, PM8606_VSYS_EN))
 675			goto out;
 676
 677		/*Enable Internal Oscillator */
 678		if (pm860x_set_bits(i2c, PM8606_MISC,
 679				PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
 680			goto out;
 681		/* Update status (only if writes succeed) */
 682		chip->osc_status = PM8606_REF_GP_OSC_ON;
 683	}
 684	mutex_unlock(&chip->osc_lock);
 685
 686	dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
 687			__func__, chip->osc_vote,
 688			chip->osc_status, ret);
 689	return 0;
 690out:
 691	mutex_unlock(&chip->osc_lock);
 692	return ret;
 693}
 694EXPORT_SYMBOL(pm8606_osc_enable);
 695
 696int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
 697{
 698	int ret = -EIO;
 699	struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
 700		chip->client : chip->companion;
 701
 702	dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
 703	dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
 704			__func__, chip->osc_vote,
 705			chip->osc_status);
 706
 707	mutex_lock(&chip->osc_lock);
 708	/* Update voting status */
 709	chip->osc_vote &= ~(client);
 710	/*
 711	 * If reference group is off and this is the last client to release
 712	 * - turn off
 713	 */
 714	if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
 715			(chip->osc_vote == REF_GP_NO_CLIENTS)) {
 716		chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
 717		/* Disable Reference group Vsys */
 718		if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
 719			goto out;
 720		/* Disable Internal Oscillator */
 721		if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
 722			goto out;
 723		chip->osc_status = PM8606_REF_GP_OSC_OFF;
 724	}
 725	mutex_unlock(&chip->osc_lock);
 726
 727	dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
 728			__func__, chip->osc_vote,
 729			chip->osc_status, ret);
 730	return 0;
 731out:
 732	mutex_unlock(&chip->osc_lock);
 733	return ret;
 734}
 735EXPORT_SYMBOL(pm8606_osc_disable);
 736
 737static void device_osc_init(struct i2c_client *i2c)
 
 738{
 739	struct pm860x_chip *chip = i2c_get_clientdata(i2c);
 
 740
 741	mutex_init(&chip->osc_lock);
 742	/* init portofino reference group voting and status */
 743	/* Disable Reference group Vsys */
 744	pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
 745	/* Disable Internal Oscillator */
 746	pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
 747
 748	chip->osc_vote = REF_GP_NO_CLIENTS;
 749	chip->osc_status = PM8606_REF_GP_OSC_OFF;
 750}
 751
 752static void device_bk_init(struct pm860x_chip *chip,
 753				     struct pm860x_platform_data *pdata)
 754{
 755	int ret, i;
 756
 757	if (pdata && pdata->backlight) {
 758		if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
 759			pdata->num_backlights = ARRAY_SIZE(bk_devs);
 760		for (i = 0; i < pdata->num_backlights; i++) {
 761			bk_devs[i].platform_data = &pdata->backlight[i];
 762			bk_devs[i].pdata_size =
 763				sizeof(struct pm860x_backlight_pdata);
 
 
 
 
 
 
 
 
 
 
 
 
 764		}
 765	}
 766	ret = mfd_add_devices(chip->dev, 0, bk_devs,
 767			      ARRAY_SIZE(bk_devs), NULL, 0, NULL);
 768	if (ret < 0)
 769		dev_err(chip->dev, "Failed to add backlight subdev\n");
 770}
 771
 772static void device_led_init(struct pm860x_chip *chip,
 773				      struct pm860x_platform_data *pdata)
 774{
 775	int ret, i;
 
 
 776
 777	if (pdata && pdata->led) {
 778		if (pdata->num_leds > ARRAY_SIZE(led_devs))
 779			pdata->num_leds = ARRAY_SIZE(led_devs);
 780		for (i = 0; i < pdata->num_leds; i++) {
 781			led_devs[i].platform_data = &pdata->led[i];
 782			led_devs[i].pdata_size =
 783				sizeof(struct pm860x_led_pdata);
 784		}
 785	}
 786	ret = mfd_add_devices(chip->dev, 0, led_devs,
 787			      ARRAY_SIZE(led_devs), NULL, 0, NULL);
 788	if (ret < 0) {
 789		dev_err(chip->dev, "Failed to add led subdev\n");
 790		return;
 791	}
 792}
 793
 794static void device_regulator_init(struct pm860x_chip *chip,
 795					    struct pm860x_platform_data *pdata)
 796{
 797	int ret;
 798
 799	if (pdata == NULL)
 800		return;
 801	if (pdata->buck1) {
 802		reg_devs[0].platform_data = pdata->buck1;
 803		reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
 804	}
 805	if (pdata->buck2) {
 806		reg_devs[1].platform_data = pdata->buck2;
 807		reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
 808	}
 809	if (pdata->buck3) {
 810		reg_devs[2].platform_data = pdata->buck3;
 811		reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
 812	}
 813	if (pdata->ldo1) {
 814		reg_devs[3].platform_data = pdata->ldo1;
 815		reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
 816	}
 817	if (pdata->ldo2) {
 818		reg_devs[4].platform_data = pdata->ldo2;
 819		reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
 820	}
 821	if (pdata->ldo3) {
 822		reg_devs[5].platform_data = pdata->ldo3;
 823		reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
 824	}
 825	if (pdata->ldo4) {
 826		reg_devs[6].platform_data = pdata->ldo4;
 827		reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
 828	}
 829	if (pdata->ldo5) {
 830		reg_devs[7].platform_data = pdata->ldo5;
 831		reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
 832	}
 833	if (pdata->ldo6) {
 834		reg_devs[8].platform_data = pdata->ldo6;
 835		reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
 836	}
 837	if (pdata->ldo7) {
 838		reg_devs[9].platform_data = pdata->ldo7;
 839		reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
 840	}
 841	if (pdata->ldo8) {
 842		reg_devs[10].platform_data = pdata->ldo8;
 843		reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
 844	}
 845	if (pdata->ldo9) {
 846		reg_devs[11].platform_data = pdata->ldo9;
 847		reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
 848	}
 849	if (pdata->ldo10) {
 850		reg_devs[12].platform_data = pdata->ldo10;
 851		reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
 852	}
 853	if (pdata->ldo12) {
 854		reg_devs[13].platform_data = pdata->ldo12;
 855		reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
 856	}
 857	if (pdata->ldo_vibrator) {
 858		reg_devs[14].platform_data = pdata->ldo_vibrator;
 859		reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
 860	}
 861	if (pdata->ldo14) {
 862		reg_devs[15].platform_data = pdata->ldo14;
 863		reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
 864	}
 865	ret = mfd_add_devices(chip->dev, 0, reg_devs,
 866			      ARRAY_SIZE(reg_devs), NULL, 0, NULL);
 867	if (ret < 0) {
 868		dev_err(chip->dev, "Failed to add regulator subdev\n");
 869		return;
 870	}
 
 
 871}
 872
 873static void device_rtc_init(struct pm860x_chip *chip,
 874				      struct pm860x_platform_data *pdata)
 875{
 876	int ret;
 877
 878	if (!pdata)
 879		return;
 880
 881	rtc_devs[0].platform_data = pdata->rtc;
 882	rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
 883	rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
 884	rtc_devs[0].resources = &rtc_resources[0];
 885	ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
 886			      ARRAY_SIZE(rtc_devs), &rtc_resources[0],
 887			      chip->irq_base, NULL);
 888	if (ret < 0)
 889		dev_err(chip->dev, "Failed to add rtc subdev\n");
 890}
 891
 892static void device_touch_init(struct pm860x_chip *chip,
 893					struct pm860x_platform_data *pdata)
 894{
 895	int ret;
 896
 897	if (pdata == NULL)
 898		return;
 899
 900	touch_devs[0].platform_data = pdata->touch;
 901	touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
 902	touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
 903	touch_devs[0].resources = &touch_resources[0];
 904	ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
 905			      ARRAY_SIZE(touch_devs), &touch_resources[0],
 906			      chip->irq_base, NULL);
 907	if (ret < 0)
 908		dev_err(chip->dev, "Failed to add touch subdev\n");
 909}
 910
 911static void device_power_init(struct pm860x_chip *chip,
 912					struct pm860x_platform_data *pdata)
 913{
 914	int ret;
 915
 916	if (pdata == NULL)
 917		return;
 918
 919	power_devs[0].platform_data = pdata->power;
 920	power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
 921	power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
 922	power_devs[0].resources = &battery_resources[0],
 923	ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
 924			      &battery_resources[0], chip->irq_base, NULL);
 925	if (ret < 0)
 926		dev_err(chip->dev, "Failed to add battery subdev\n");
 927
 928	power_devs[1].platform_data = pdata->power;
 929	power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
 930	power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
 931	power_devs[1].resources = &charger_resources[0],
 932	ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
 933			      &charger_resources[0], chip->irq_base, NULL);
 934	if (ret < 0)
 935		dev_err(chip->dev, "Failed to add charger subdev\n");
 936
 937	power_devs[2].platform_data = &preg_init_data;
 938	power_devs[2].pdata_size = sizeof(struct regulator_init_data);
 939	ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
 940			      NULL, chip->irq_base, NULL);
 941	if (ret < 0)
 942		dev_err(chip->dev, "Failed to add preg subdev\n");
 943
 944	if (pdata->chg_desc) {
 945		pdata->chg_desc->charger_regulators =
 946			&chg_desc_regulator_data[0];
 947		pdata->chg_desc->num_charger_regulators	=
 948			ARRAY_SIZE(chg_desc_regulator_data),
 949		power_devs[3].platform_data = pdata->chg_desc;
 950		power_devs[3].pdata_size = sizeof(*pdata->chg_desc);
 951		ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1,
 952				      NULL, chip->irq_base, NULL);
 953		if (ret < 0)
 954			dev_err(chip->dev, "Failed to add chg-manager subdev\n");
 955	}
 956}
 957
 958static void device_onkey_init(struct pm860x_chip *chip,
 959					struct pm860x_platform_data *pdata)
 960{
 961	int ret;
 962
 963	onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
 964	onkey_devs[0].resources = &onkey_resources[0],
 965	ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
 966			      ARRAY_SIZE(onkey_devs), &onkey_resources[0],
 967			      chip->irq_base, NULL);
 968	if (ret < 0)
 969		dev_err(chip->dev, "Failed to add onkey subdev\n");
 970}
 971
 972static void device_codec_init(struct pm860x_chip *chip,
 973					struct pm860x_platform_data *pdata)
 974{
 975	int ret;
 976
 977	codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
 978	codec_devs[0].resources = &codec_resources[0],
 979	ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
 980			      ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
 981			      NULL);
 982	if (ret < 0)
 983		dev_err(chip->dev, "Failed to add codec subdev\n");
 984}
 985
 986static void device_8607_init(struct pm860x_chip *chip,
 987				       struct i2c_client *i2c,
 988				       struct pm860x_platform_data *pdata)
 989{
 990	int data, ret;
 991
 992	ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
 993	if (ret < 0) {
 994		dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
 995		goto out;
 996	}
 997	switch (ret & PM8607_VERSION_MASK) {
 998	case 0x40:
 999	case 0x50:
1000		dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
1001			 ret);
1002		break;
1003	default:
1004		dev_err(chip->dev,
1005			"Failed to detect Marvell 88PM8607. Chip ID: %02x\n",
1006			ret);
1007		goto out;
1008	}
1009
1010	ret = pm860x_reg_read(i2c, PM8607_BUCK3);
1011	if (ret < 0) {
1012		dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
1013		goto out;
1014	}
1015	if (ret & PM8607_BUCK3_DOUBLE)
1016		chip->buck3_double = 1;
1017
1018	ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
1019	if (ret < 0) {
1020		dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
1021		goto out;
1022	}
1023
1024	if (pdata && (pdata->i2c_port == PI2C_PORT))
1025		data = PM8607_B0_MISC1_PI2C;
1026	else
1027		data = 0;
1028	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
1029	if (ret < 0) {
1030		dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
1031		goto out;
1032	}
1033
 
 
 
 
1034	ret = device_irq_init(chip, pdata);
1035	if (ret < 0)
1036		goto out;
1037
1038	device_regulator_init(chip, pdata);
1039	device_rtc_init(chip, pdata);
1040	device_onkey_init(chip, pdata);
1041	device_touch_init(chip, pdata);
1042	device_power_init(chip, pdata);
1043	device_codec_init(chip, pdata);
1044out:
1045	return;
1046}
1047
1048static void device_8606_init(struct pm860x_chip *chip,
1049				       struct i2c_client *i2c,
1050				       struct pm860x_platform_data *pdata)
1051{
1052	device_osc_init(i2c);
1053	device_bk_init(chip, pdata);
1054	device_led_init(chip, pdata);
1055}
1056
1057static int pm860x_device_init(struct pm860x_chip *chip,
1058					struct pm860x_platform_data *pdata)
1059{
1060	chip->core_irq = 0;
1061
1062	switch (chip->id) {
1063	case CHIP_PM8606:
1064		device_8606_init(chip, chip->client, pdata);
 
1065		break;
1066	case CHIP_PM8607:
1067		device_8607_init(chip, chip->client, pdata);
1068		break;
1069	}
1070
1071	if (chip->companion) {
1072		switch (chip->id) {
1073		case CHIP_PM8607:
1074			device_8606_init(chip, chip->companion, pdata);
 
1075			break;
1076		case CHIP_PM8606:
1077			device_8607_init(chip, chip->companion, pdata);
1078			break;
1079		}
1080	}
1081
1082	return 0;
1083}
1084
1085static void pm860x_device_exit(struct pm860x_chip *chip)
1086{
1087	device_irq_exit(chip);
1088	mfd_remove_devices(chip->dev);
1089}
1090
1091static int verify_addr(struct i2c_client *i2c)
1092{
1093	unsigned short addr_8607[] = {0x30, 0x34};
1094	unsigned short addr_8606[] = {0x10, 0x11};
1095	int size, i;
1096
1097	if (i2c == NULL)
1098		return 0;
1099	size = ARRAY_SIZE(addr_8606);
1100	for (i = 0; i < size; i++) {
1101		if (i2c->addr == *(addr_8606 + i))
1102			return CHIP_PM8606;
1103	}
1104	size = ARRAY_SIZE(addr_8607);
1105	for (i = 0; i < size; i++) {
1106		if (i2c->addr == *(addr_8607 + i))
1107			return CHIP_PM8607;
1108	}
1109	return 0;
1110}
1111
1112static const struct regmap_config pm860x_regmap_config = {
1113	.reg_bits = 8,
1114	.val_bits = 8,
1115};
1116
1117static int pm860x_dt_init(struct device_node *np,
1118				    struct device *dev,
1119				    struct pm860x_platform_data *pdata)
1120{
1121	int ret;
1122
1123	if (of_get_property(np, "marvell,88pm860x-irq-read-clr", NULL))
1124		pdata->irq_mode = 1;
1125	ret = of_property_read_u32(np, "marvell,88pm860x-slave-addr",
1126				   &pdata->companion_addr);
1127	if (ret) {
1128		dev_err(dev,
1129			"Not found \"marvell,88pm860x-slave-addr\" property\n");
1130		pdata->companion_addr = 0;
1131	}
1132	return 0;
1133}
1134
1135static int pm860x_probe(struct i2c_client *client)
1136{
1137	struct pm860x_platform_data *pdata = dev_get_platdata(&client->dev);
1138	struct device_node *node = client->dev.of_node;
1139	struct pm860x_chip *chip;
1140	int ret;
1141
1142	if (node && !pdata) {
1143		/* parse DT to get platform data */
1144		pdata = devm_kzalloc(&client->dev,
1145				     sizeof(struct pm860x_platform_data),
1146				     GFP_KERNEL);
1147		if (!pdata)
1148			return -ENOMEM;
1149		ret = pm860x_dt_init(node, &client->dev, pdata);
1150		if (ret)
1151			return ret;
1152	} else if (!pdata) {
1153		pr_info("No platform data in %s!\n", __func__);
1154		return -EINVAL;
1155	}
1156
1157	chip = devm_kzalloc(&client->dev,
1158			    sizeof(struct pm860x_chip), GFP_KERNEL);
1159	if (chip == NULL)
1160		return -ENOMEM;
1161
1162	chip->id = verify_addr(client);
1163	chip->regmap = devm_regmap_init_i2c(client, &pm860x_regmap_config);
1164	if (IS_ERR(chip->regmap)) {
1165		ret = PTR_ERR(chip->regmap);
1166		dev_err(&client->dev, "Failed to allocate register map: %d\n",
1167				ret);
1168		return ret;
1169	}
1170	chip->client = client;
1171	i2c_set_clientdata(client, chip);
1172	chip->dev = &client->dev;
1173	dev_set_drvdata(chip->dev, chip);
1174
1175	/*
1176	 * Both client and companion client shares same platform driver.
1177	 * Driver distinguishes them by pdata->companion_addr.
1178	 * pdata->companion_addr is only assigned if companion chip exists.
1179	 * At the same time, the companion_addr shouldn't equal to client
1180	 * address.
1181	 */
1182	if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
1183		chip->companion_addr = pdata->companion_addr;
1184		chip->companion = i2c_new_dummy(chip->client->adapter,
1185						chip->companion_addr);
1186		if (!chip->companion) {
1187			dev_err(&client->dev,
1188				"Failed to allocate I2C companion device\n");
1189			return -ENODEV;
1190		}
1191		chip->regmap_companion = regmap_init_i2c(chip->companion,
1192							&pm860x_regmap_config);
1193		if (IS_ERR(chip->regmap_companion)) {
1194			ret = PTR_ERR(chip->regmap_companion);
1195			dev_err(&chip->companion->dev,
1196				"Failed to allocate register map: %d\n", ret);
1197			i2c_unregister_device(chip->companion);
1198			return ret;
1199		}
1200		i2c_set_clientdata(chip->companion, chip);
1201	}
1202
1203	pm860x_device_init(chip, pdata);
1204	return 0;
1205}
1206
1207static int pm860x_remove(struct i2c_client *client)
1208{
1209	struct pm860x_chip *chip = i2c_get_clientdata(client);
1210
1211	pm860x_device_exit(chip);
1212	if (chip->companion) {
1213		regmap_exit(chip->regmap_companion);
1214		i2c_unregister_device(chip->companion);
1215	}
1216	return 0;
1217}
1218
1219#ifdef CONFIG_PM_SLEEP
1220static int pm860x_suspend(struct device *dev)
1221{
1222	struct i2c_client *client = to_i2c_client(dev);
1223	struct pm860x_chip *chip = i2c_get_clientdata(client);
1224
1225	if (device_may_wakeup(dev) && chip->wakeup_flag)
1226		enable_irq_wake(chip->core_irq);
1227	return 0;
1228}
1229
1230static int pm860x_resume(struct device *dev)
1231{
1232	struct i2c_client *client = to_i2c_client(dev);
1233	struct pm860x_chip *chip = i2c_get_clientdata(client);
1234
1235	if (device_may_wakeup(dev) && chip->wakeup_flag)
1236		disable_irq_wake(chip->core_irq);
1237	return 0;
1238}
1239#endif
1240
1241static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
1242
1243static const struct i2c_device_id pm860x_id_table[] = {
1244	{ "88PM860x", 0 },
1245	{}
1246};
1247MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
1248
1249static const struct of_device_id pm860x_dt_ids[] = {
1250	{ .compatible = "marvell,88pm860x", },
1251	{},
1252};
1253MODULE_DEVICE_TABLE(of, pm860x_dt_ids);
1254
1255static struct i2c_driver pm860x_driver = {
1256	.driver	= {
1257		.name	= "88PM860x",
1258		.pm     = &pm860x_pm_ops,
1259		.of_match_table	= pm860x_dt_ids,
1260	},
1261	.probe_new	= pm860x_probe,
1262	.remove		= pm860x_remove,
1263	.id_table	= pm860x_id_table,
1264};
1265
1266static int __init pm860x_i2c_init(void)
1267{
1268	int ret;
1269
1270	ret = i2c_add_driver(&pm860x_driver);
1271	if (ret != 0)
1272		pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
1273	return ret;
1274}
1275subsys_initcall(pm860x_i2c_init);
1276
1277static void __exit pm860x_i2c_exit(void)
1278{
1279	i2c_del_driver(&pm860x_driver);
1280}
1281module_exit(pm860x_i2c_exit);
1282
1283MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
1284MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1285MODULE_LICENSE("GPL");