xref: /openbmc/linux/drivers/mfd/tps6586x.c (revision b9ccfda2)
1 /*
2  * Core driver for TI TPS6586x PMIC family
3  *
4  * Copyright (c) 2010 CompuLab Ltd.
5  * Mike Rapoport <mike@compulab.co.il>
6  *
7  * Based on da903x.c.
8  * Copyright (C) 2008 Compulab, Ltd.
9  * Mike Rapoport <mike@compulab.co.il>
10  * Copyright (C) 2006-2008 Marvell International Ltd.
11  * Eric Miao <eric.miao@marvell.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17 
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
25 #include <linux/i2c.h>
26 #include <linux/regulator/of_regulator.h>
27 
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/tps6586x.h>
30 
31 /* GPIO control registers */
32 #define TPS6586X_GPIOSET1	0x5d
33 #define TPS6586X_GPIOSET2	0x5e
34 
35 /* interrupt control registers */
36 #define TPS6586X_INT_ACK1	0xb5
37 #define TPS6586X_INT_ACK2	0xb6
38 #define TPS6586X_INT_ACK3	0xb7
39 #define TPS6586X_INT_ACK4	0xb8
40 
41 /* interrupt mask registers */
42 #define TPS6586X_INT_MASK1	0xb0
43 #define TPS6586X_INT_MASK2	0xb1
44 #define TPS6586X_INT_MASK3	0xb2
45 #define TPS6586X_INT_MASK4	0xb3
46 #define TPS6586X_INT_MASK5	0xb4
47 
48 /* device id */
49 #define TPS6586X_VERSIONCRC	0xcd
50 
51 struct tps6586x_irq_data {
52 	u8	mask_reg;
53 	u8	mask_mask;
54 };
55 
56 #define TPS6586X_IRQ(_reg, _mask)				\
57 	{							\
58 		.mask_reg = (_reg) - TPS6586X_INT_MASK1,	\
59 		.mask_mask = (_mask),				\
60 	}
61 
62 static const struct tps6586x_irq_data tps6586x_irqs[] = {
63 	[TPS6586X_INT_PLDO_0]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
64 	[TPS6586X_INT_PLDO_1]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
65 	[TPS6586X_INT_PLDO_2]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
66 	[TPS6586X_INT_PLDO_3]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
67 	[TPS6586X_INT_PLDO_4]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
68 	[TPS6586X_INT_PLDO_5]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
69 	[TPS6586X_INT_PLDO_6]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
70 	[TPS6586X_INT_PLDO_7]	= TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
71 	[TPS6586X_INT_COMP_DET]	= TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
72 	[TPS6586X_INT_ADC]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
73 	[TPS6586X_INT_PLDO_8]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
74 	[TPS6586X_INT_PLDO_9]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
75 	[TPS6586X_INT_PSM_0]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
76 	[TPS6586X_INT_PSM_1]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
77 	[TPS6586X_INT_PSM_2]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
78 	[TPS6586X_INT_PSM_3]	= TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
79 	[TPS6586X_INT_RTC_ALM1]	= TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
80 	[TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
81 	[TPS6586X_INT_USB_DET]	= TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
82 	[TPS6586X_INT_AC_DET]	= TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
83 	[TPS6586X_INT_BAT_DET]	= TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
84 	[TPS6586X_INT_CHG_STAT]	= TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
85 	[TPS6586X_INT_CHG_TEMP]	= TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
86 	[TPS6586X_INT_PP]	= TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
87 	[TPS6586X_INT_RESUME]	= TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
88 	[TPS6586X_INT_LOW_SYS]	= TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
89 	[TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
90 };
91 
92 struct tps6586x {
93 	struct mutex		lock;
94 	struct device		*dev;
95 	struct i2c_client	*client;
96 
97 	struct gpio_chip	gpio;
98 	struct irq_chip		irq_chip;
99 	struct mutex		irq_lock;
100 	int			irq_base;
101 	u32			irq_en;
102 	u8			mask_cache[5];
103 	u8			mask_reg[5];
104 };
105 
106 static inline int __tps6586x_read(struct i2c_client *client,
107 				  int reg, uint8_t *val)
108 {
109 	int ret;
110 
111 	ret = i2c_smbus_read_byte_data(client, reg);
112 	if (ret < 0) {
113 		dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
114 		return ret;
115 	}
116 
117 	*val = (uint8_t)ret;
118 
119 	return 0;
120 }
121 
122 static inline int __tps6586x_reads(struct i2c_client *client, int reg,
123 				   int len, uint8_t *val)
124 {
125 	int ret;
126 
127 	ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
128 	if (ret < 0) {
129 		dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
130 		return ret;
131 	}
132 
133 	return 0;
134 }
135 
136 static inline int __tps6586x_write(struct i2c_client *client,
137 				 int reg, uint8_t val)
138 {
139 	int ret;
140 
141 	ret = i2c_smbus_write_byte_data(client, reg, val);
142 	if (ret < 0) {
143 		dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
144 				val, reg);
145 		return ret;
146 	}
147 
148 	return 0;
149 }
150 
151 static inline int __tps6586x_writes(struct i2c_client *client, int reg,
152 				  int len, uint8_t *val)
153 {
154 	int ret, i;
155 
156 	for (i = 0; i < len; i++) {
157 		ret = __tps6586x_write(client, reg + i, *(val + i));
158 		if (ret < 0)
159 			return ret;
160 	}
161 
162 	return 0;
163 }
164 
165 int tps6586x_write(struct device *dev, int reg, uint8_t val)
166 {
167 	return __tps6586x_write(to_i2c_client(dev), reg, val);
168 }
169 EXPORT_SYMBOL_GPL(tps6586x_write);
170 
171 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
172 {
173 	return __tps6586x_writes(to_i2c_client(dev), reg, len, val);
174 }
175 EXPORT_SYMBOL_GPL(tps6586x_writes);
176 
177 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
178 {
179 	return __tps6586x_read(to_i2c_client(dev), reg, val);
180 }
181 EXPORT_SYMBOL_GPL(tps6586x_read);
182 
183 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
184 {
185 	return __tps6586x_reads(to_i2c_client(dev), reg, len, val);
186 }
187 EXPORT_SYMBOL_GPL(tps6586x_reads);
188 
189 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
190 {
191 	struct tps6586x *tps6586x = dev_get_drvdata(dev);
192 	uint8_t reg_val;
193 	int ret = 0;
194 
195 	mutex_lock(&tps6586x->lock);
196 
197 	ret = __tps6586x_read(to_i2c_client(dev), reg, &reg_val);
198 	if (ret)
199 		goto out;
200 
201 	if ((reg_val & bit_mask) != bit_mask) {
202 		reg_val |= bit_mask;
203 		ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val);
204 	}
205 out:
206 	mutex_unlock(&tps6586x->lock);
207 	return ret;
208 }
209 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
210 
211 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
212 {
213 	struct tps6586x *tps6586x = dev_get_drvdata(dev);
214 	uint8_t reg_val;
215 	int ret = 0;
216 
217 	mutex_lock(&tps6586x->lock);
218 
219 	ret = __tps6586x_read(to_i2c_client(dev), reg, &reg_val);
220 	if (ret)
221 		goto out;
222 
223 	if (reg_val & bit_mask) {
224 		reg_val &= ~bit_mask;
225 		ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val);
226 	}
227 out:
228 	mutex_unlock(&tps6586x->lock);
229 	return ret;
230 }
231 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
232 
233 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
234 {
235 	struct tps6586x *tps6586x = dev_get_drvdata(dev);
236 	uint8_t reg_val;
237 	int ret = 0;
238 
239 	mutex_lock(&tps6586x->lock);
240 
241 	ret = __tps6586x_read(tps6586x->client, reg, &reg_val);
242 	if (ret)
243 		goto out;
244 
245 	if ((reg_val & mask) != val) {
246 		reg_val = (reg_val & ~mask) | val;
247 		ret = __tps6586x_write(tps6586x->client, reg, reg_val);
248 	}
249 out:
250 	mutex_unlock(&tps6586x->lock);
251 	return ret;
252 }
253 EXPORT_SYMBOL_GPL(tps6586x_update);
254 
255 static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
256 {
257 	struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio);
258 	uint8_t val;
259 	int ret;
260 
261 	ret = __tps6586x_read(tps6586x->client, TPS6586X_GPIOSET2, &val);
262 	if (ret)
263 		return ret;
264 
265 	return !!(val & (1 << offset));
266 }
267 
268 
269 static void tps6586x_gpio_set(struct gpio_chip *chip, unsigned offset,
270 			      int value)
271 {
272 	struct tps6586x *tps6586x = container_of(chip, struct tps6586x, gpio);
273 
274 	tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET2,
275 			value << offset, 1 << offset);
276 }
277 
278 static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
279 				int value)
280 {
281 	struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio);
282 	uint8_t val, mask;
283 
284 	tps6586x_gpio_set(gc, offset, value);
285 
286 	val = 0x1 << (offset * 2);
287 	mask = 0x3 << (offset * 2);
288 
289 	return tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET1, val, mask);
290 }
291 
292 static int tps6586x_gpio_init(struct tps6586x *tps6586x, int gpio_base)
293 {
294 	if (!gpio_base)
295 		return 0;
296 
297 	tps6586x->gpio.owner		= THIS_MODULE;
298 	tps6586x->gpio.label		= tps6586x->client->name;
299 	tps6586x->gpio.dev		= tps6586x->dev;
300 	tps6586x->gpio.base		= gpio_base;
301 	tps6586x->gpio.ngpio		= 4;
302 	tps6586x->gpio.can_sleep	= 1;
303 
304 	/* FIXME: add handling of GPIOs as dedicated inputs */
305 	tps6586x->gpio.direction_output	= tps6586x_gpio_output;
306 	tps6586x->gpio.set		= tps6586x_gpio_set;
307 	tps6586x->gpio.get		= tps6586x_gpio_get;
308 
309 	return gpiochip_add(&tps6586x->gpio);
310 }
311 
312 static int __remove_subdev(struct device *dev, void *unused)
313 {
314 	platform_device_unregister(to_platform_device(dev));
315 	return 0;
316 }
317 
318 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
319 {
320 	return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
321 }
322 
323 static void tps6586x_irq_lock(struct irq_data *data)
324 {
325 	struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
326 
327 	mutex_lock(&tps6586x->irq_lock);
328 }
329 
330 static void tps6586x_irq_enable(struct irq_data *irq_data)
331 {
332 	struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
333 	unsigned int __irq = irq_data->irq - tps6586x->irq_base;
334 	const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
335 
336 	tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
337 	tps6586x->irq_en |= (1 << __irq);
338 }
339 
340 static void tps6586x_irq_disable(struct irq_data *irq_data)
341 {
342 	struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
343 
344 	unsigned int __irq = irq_data->irq - tps6586x->irq_base;
345 	const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
346 
347 	tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
348 	tps6586x->irq_en &= ~(1 << __irq);
349 }
350 
351 static void tps6586x_irq_sync_unlock(struct irq_data *data)
352 {
353 	struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
354 	int i;
355 
356 	for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
357 		if (tps6586x->mask_reg[i] != tps6586x->mask_cache[i]) {
358 			if (!WARN_ON(tps6586x_write(tps6586x->dev,
359 						    TPS6586X_INT_MASK1 + i,
360 						    tps6586x->mask_reg[i])))
361 				tps6586x->mask_cache[i] = tps6586x->mask_reg[i];
362 		}
363 	}
364 
365 	mutex_unlock(&tps6586x->irq_lock);
366 }
367 
368 static irqreturn_t tps6586x_irq(int irq, void *data)
369 {
370 	struct tps6586x *tps6586x = data;
371 	u32 acks;
372 	int ret = 0;
373 
374 	ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
375 			     sizeof(acks), (uint8_t *)&acks);
376 
377 	if (ret < 0) {
378 		dev_err(tps6586x->dev, "failed to read interrupt status\n");
379 		return IRQ_NONE;
380 	}
381 
382 	acks = le32_to_cpu(acks);
383 
384 	while (acks) {
385 		int i = __ffs(acks);
386 
387 		if (tps6586x->irq_en & (1 << i))
388 			handle_nested_irq(tps6586x->irq_base + i);
389 
390 		acks &= ~(1 << i);
391 	}
392 
393 	return IRQ_HANDLED;
394 }
395 
396 static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
397 				       int irq_base)
398 {
399 	int i, ret;
400 	u8 tmp[4];
401 
402 	if (!irq_base) {
403 		dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
404 		return -EINVAL;
405 	}
406 
407 	mutex_init(&tps6586x->irq_lock);
408 	for (i = 0; i < 5; i++) {
409 		tps6586x->mask_cache[i] = 0xff;
410 		tps6586x->mask_reg[i] = 0xff;
411 		tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
412 	}
413 
414 	tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
415 
416 	tps6586x->irq_base = irq_base;
417 
418 	tps6586x->irq_chip.name = "tps6586x";
419 	tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
420 	tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
421 	tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
422 	tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
423 
424 	for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
425 		int __irq = i + tps6586x->irq_base;
426 		irq_set_chip_data(__irq, tps6586x);
427 		irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
428 					 handle_simple_irq);
429 		irq_set_nested_thread(__irq, 1);
430 #ifdef CONFIG_ARM
431 		set_irq_flags(__irq, IRQF_VALID);
432 #endif
433 	}
434 
435 	ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
436 				   "tps6586x", tps6586x);
437 
438 	if (!ret) {
439 		device_init_wakeup(tps6586x->dev, 1);
440 		enable_irq_wake(irq);
441 	}
442 
443 	return ret;
444 }
445 
446 static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
447 					  struct tps6586x_platform_data *pdata)
448 {
449 	struct tps6586x_subdev_info *subdev;
450 	struct platform_device *pdev;
451 	int i, ret = 0;
452 
453 	for (i = 0; i < pdata->num_subdevs; i++) {
454 		subdev = &pdata->subdevs[i];
455 
456 		pdev = platform_device_alloc(subdev->name, subdev->id);
457 		if (!pdev) {
458 			ret = -ENOMEM;
459 			goto failed;
460 		}
461 
462 		pdev->dev.parent = tps6586x->dev;
463 		pdev->dev.platform_data = subdev->platform_data;
464 		pdev->dev.of_node = subdev->of_node;
465 
466 		ret = platform_device_add(pdev);
467 		if (ret) {
468 			platform_device_put(pdev);
469 			goto failed;
470 		}
471 	}
472 	return 0;
473 
474 failed:
475 	tps6586x_remove_subdevs(tps6586x);
476 	return ret;
477 }
478 
479 #ifdef CONFIG_OF
480 static struct of_regulator_match tps6586x_matches[] = {
481 	{ .name = "sm0",     .driver_data = (void *)TPS6586X_ID_SM_0    },
482 	{ .name = "sm1",     .driver_data = (void *)TPS6586X_ID_SM_1    },
483 	{ .name = "sm2",     .driver_data = (void *)TPS6586X_ID_SM_2    },
484 	{ .name = "ldo0",    .driver_data = (void *)TPS6586X_ID_LDO_0   },
485 	{ .name = "ldo1",    .driver_data = (void *)TPS6586X_ID_LDO_1   },
486 	{ .name = "ldo2",    .driver_data = (void *)TPS6586X_ID_LDO_2   },
487 	{ .name = "ldo3",    .driver_data = (void *)TPS6586X_ID_LDO_3   },
488 	{ .name = "ldo4",    .driver_data = (void *)TPS6586X_ID_LDO_4   },
489 	{ .name = "ldo5",    .driver_data = (void *)TPS6586X_ID_LDO_5   },
490 	{ .name = "ldo6",    .driver_data = (void *)TPS6586X_ID_LDO_6   },
491 	{ .name = "ldo7",    .driver_data = (void *)TPS6586X_ID_LDO_7   },
492 	{ .name = "ldo8",    .driver_data = (void *)TPS6586X_ID_LDO_8   },
493 	{ .name = "ldo9",    .driver_data = (void *)TPS6586X_ID_LDO_9   },
494 	{ .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
495 };
496 
497 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
498 {
499 	const unsigned int num = ARRAY_SIZE(tps6586x_matches);
500 	struct device_node *np = client->dev.of_node;
501 	struct tps6586x_platform_data *pdata;
502 	struct tps6586x_subdev_info *devs;
503 	struct device_node *regs;
504 	unsigned int count;
505 	unsigned int i, j;
506 	int err;
507 
508 	regs = of_find_node_by_name(np, "regulators");
509 	if (!regs)
510 		return NULL;
511 
512 	err = of_regulator_match(&client->dev, regs, tps6586x_matches, num);
513 	if (err < 0) {
514 		of_node_put(regs);
515 		return NULL;
516 	}
517 
518 	of_node_put(regs);
519 	count = err;
520 
521 	devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL);
522 	if (!devs)
523 		return NULL;
524 
525 	for (i = 0, j = 0; i < num && j < count; i++) {
526 		if (!tps6586x_matches[i].init_data)
527 			continue;
528 
529 		devs[j].name = "tps6586x-regulator";
530 		devs[j].platform_data = tps6586x_matches[i].init_data;
531 		devs[j].id = (int)tps6586x_matches[i].driver_data;
532 		devs[j].of_node = tps6586x_matches[i].of_node;
533 		j++;
534 	}
535 
536 	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
537 	if (!pdata)
538 		return NULL;
539 
540 	pdata->num_subdevs = count;
541 	pdata->subdevs = devs;
542 	pdata->gpio_base = -1;
543 	pdata->irq_base = -1;
544 
545 	return pdata;
546 }
547 
548 static struct of_device_id tps6586x_of_match[] = {
549 	{ .compatible = "ti,tps6586x", },
550 	{ },
551 };
552 #else
553 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
554 {
555 	return NULL;
556 }
557 #endif
558 
559 static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
560 					const struct i2c_device_id *id)
561 {
562 	struct tps6586x_platform_data *pdata = client->dev.platform_data;
563 	struct tps6586x *tps6586x;
564 	int ret;
565 
566 	if (!pdata && client->dev.of_node)
567 		pdata = tps6586x_parse_dt(client);
568 
569 	if (!pdata) {
570 		dev_err(&client->dev, "tps6586x requires platform data\n");
571 		return -ENOTSUPP;
572 	}
573 
574 	ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
575 	if (ret < 0) {
576 		dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
577 		return -EIO;
578 	}
579 
580 	dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
581 
582 	tps6586x = kzalloc(sizeof(struct tps6586x), GFP_KERNEL);
583 	if (tps6586x == NULL)
584 		return -ENOMEM;
585 
586 	tps6586x->client = client;
587 	tps6586x->dev = &client->dev;
588 	i2c_set_clientdata(client, tps6586x);
589 
590 	mutex_init(&tps6586x->lock);
591 
592 	if (client->irq) {
593 		ret = tps6586x_irq_init(tps6586x, client->irq,
594 					pdata->irq_base);
595 		if (ret) {
596 			dev_err(&client->dev, "IRQ init failed: %d\n", ret);
597 			goto err_irq_init;
598 		}
599 	}
600 
601 	ret = tps6586x_gpio_init(tps6586x, pdata->gpio_base);
602 	if (ret) {
603 		dev_err(&client->dev, "GPIO registration failed: %d\n", ret);
604 		goto err_gpio_init;
605 	}
606 
607 	ret = tps6586x_add_subdevs(tps6586x, pdata);
608 	if (ret) {
609 		dev_err(&client->dev, "add devices failed: %d\n", ret);
610 		goto err_add_devs;
611 	}
612 
613 	return 0;
614 
615 err_add_devs:
616 	if (pdata->gpio_base) {
617 		ret = gpiochip_remove(&tps6586x->gpio);
618 		if (ret)
619 			dev_err(&client->dev, "Can't remove gpio chip: %d\n",
620 				ret);
621 	}
622 err_gpio_init:
623 	if (client->irq)
624 		free_irq(client->irq, tps6586x);
625 err_irq_init:
626 	kfree(tps6586x);
627 	return ret;
628 }
629 
630 static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
631 {
632 	struct tps6586x *tps6586x = i2c_get_clientdata(client);
633 	struct tps6586x_platform_data *pdata = client->dev.platform_data;
634 	int ret;
635 
636 	if (client->irq)
637 		free_irq(client->irq, tps6586x);
638 
639 	if (pdata->gpio_base) {
640 		ret = gpiochip_remove(&tps6586x->gpio);
641 		if (ret)
642 			dev_err(&client->dev, "Can't remove gpio chip: %d\n",
643 				ret);
644 	}
645 
646 	tps6586x_remove_subdevs(tps6586x);
647 	kfree(tps6586x);
648 	return 0;
649 }
650 
651 static const struct i2c_device_id tps6586x_id_table[] = {
652 	{ "tps6586x", 0 },
653 	{ },
654 };
655 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
656 
657 static struct i2c_driver tps6586x_driver = {
658 	.driver	= {
659 		.name	= "tps6586x",
660 		.owner	= THIS_MODULE,
661 		.of_match_table = of_match_ptr(tps6586x_of_match),
662 	},
663 	.probe		= tps6586x_i2c_probe,
664 	.remove		= __devexit_p(tps6586x_i2c_remove),
665 	.id_table	= tps6586x_id_table,
666 };
667 
668 static int __init tps6586x_init(void)
669 {
670 	return i2c_add_driver(&tps6586x_driver);
671 }
672 subsys_initcall(tps6586x_init);
673 
674 static void __exit tps6586x_exit(void)
675 {
676 	i2c_del_driver(&tps6586x_driver);
677 }
678 module_exit(tps6586x_exit);
679 
680 MODULE_DESCRIPTION("TPS6586X core driver");
681 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
682 MODULE_LICENSE("GPL");
683