Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tps6586x.c
Go to the documentation of this file.
1 /*
2  * Core driver for TI TPS6586x PMIC family
3  *
4  * Copyright (c) 2010 CompuLab Ltd.
5  * Mike Rapoport <[email protected]>
6  *
7  * Based on da903x.c.
8  * Copyright (C) 2008 Compulab, Ltd.
9  * Mike Rapoport <[email protected]>
10  * Copyright (C) 2006-2008 Marvell International Ltd.
11  * Eric Miao <[email protected]>
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/err.h>
25 #include <linux/i2c.h>
26 #include <linux/regmap.h>
29 
30 #include <linux/mfd/core.h>
31 #include <linux/mfd/tps6586x.h>
32 
33 #define TPS6586X_SUPPLYENE 0x14
34 #define EXITSLREQ_BIT BIT(1)
35 #define SLEEP_MODE_BIT BIT(3)
36 
37 /* interrupt control registers */
38 #define TPS6586X_INT_ACK1 0xb5
39 #define TPS6586X_INT_ACK2 0xb6
40 #define TPS6586X_INT_ACK3 0xb7
41 #define TPS6586X_INT_ACK4 0xb8
42 
43 /* interrupt mask registers */
44 #define TPS6586X_INT_MASK1 0xb0
45 #define TPS6586X_INT_MASK2 0xb1
46 #define TPS6586X_INT_MASK3 0xb2
47 #define TPS6586X_INT_MASK4 0xb3
48 #define TPS6586X_INT_MASK5 0xb4
49 
50 /* device id */
51 #define TPS6586X_VERSIONCRC 0xcd
52 
53 /* Maximum register */
54 #define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
55 
59 };
60 
61 #define TPS6586X_IRQ(_reg, _mask) \
62  { \
63  .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
64  .mask_mask = (_mask), \
65  }
66 
67 static const struct tps6586x_irq_data tps6586x_irqs[] = {
95 };
96 
97 static struct mfd_cell tps6586x_cell[] = {
98  {
99  .name = "tps6586x-gpio",
100  },
101  {
102  .name = "tps6586x-rtc",
103  },
104  {
105  .name = "tps6586x-onkey",
106  },
107 };
108 
109 struct tps6586x {
110  struct device *dev;
112  struct regmap *regmap;
113 
115  struct mutex irq_lock;
116  int irq_base;
119 };
120 
121 static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
122 {
123  return i2c_get_clientdata(to_i2c_client(dev));
124 }
125 
127 {
128  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
129 
130  return regmap_write(tps6586x->regmap, reg, val);
131 }
133 
134 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
135 {
136  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
137 
138  return regmap_bulk_write(tps6586x->regmap, reg, val, len);
139 }
141 
142 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
143 {
144  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
145  unsigned int rval;
146  int ret;
147 
148  ret = regmap_read(tps6586x->regmap, reg, &rval);
149  if (!ret)
150  *val = rval;
151  return ret;
152 }
154 
155 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
156 {
157  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
158 
159  return regmap_bulk_read(tps6586x->regmap, reg, val, len);
160 }
162 
163 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
164 {
165  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
166 
167  return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
168 }
170 
171 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
172 {
173  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
174 
175  return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
176 }
178 
180 {
181  struct tps6586x *tps6586x = dev_to_tps6586x(dev);
182 
183  return regmap_update_bits(tps6586x->regmap, reg, mask, val);
184 }
186 
187 static int __remove_subdev(struct device *dev, void *unused)
188 {
190  return 0;
191 }
192 
193 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
194 {
195  return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
196 }
197 
198 static void tps6586x_irq_lock(struct irq_data *data)
199 {
200  struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
201 
202  mutex_lock(&tps6586x->irq_lock);
203 }
204 
205 static void tps6586x_irq_enable(struct irq_data *irq_data)
206 {
207  struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
208  unsigned int __irq = irq_data->irq - tps6586x->irq_base;
209  const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
210 
211  tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
212  tps6586x->irq_en |= (1 << __irq);
213 }
214 
215 static void tps6586x_irq_disable(struct irq_data *irq_data)
216 {
217  struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
218 
219  unsigned int __irq = irq_data->irq - tps6586x->irq_base;
220  const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
221 
222  tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
223  tps6586x->irq_en &= ~(1 << __irq);
224 }
225 
226 static void tps6586x_irq_sync_unlock(struct irq_data *data)
227 {
228  struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
229  int i;
230 
231  for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
232  int ret;
233  ret = tps6586x_write(tps6586x->dev,
234  TPS6586X_INT_MASK1 + i,
235  tps6586x->mask_reg[i]);
236  WARN_ON(ret);
237  }
238 
239  mutex_unlock(&tps6586x->irq_lock);
240 }
241 
242 static irqreturn_t tps6586x_irq(int irq, void *data)
243 {
244  struct tps6586x *tps6586x = data;
245  u32 acks;
246  int ret = 0;
247 
248  ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
249  sizeof(acks), (uint8_t *)&acks);
250 
251  if (ret < 0) {
252  dev_err(tps6586x->dev, "failed to read interrupt status\n");
253  return IRQ_NONE;
254  }
255 
256  acks = le32_to_cpu(acks);
257 
258  while (acks) {
259  int i = __ffs(acks);
260 
261  if (tps6586x->irq_en & (1 << i))
262  handle_nested_irq(tps6586x->irq_base + i);
263 
264  acks &= ~(1 << i);
265  }
266 
267  return IRQ_HANDLED;
268 }
269 
270 static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
271  int irq_base)
272 {
273  int i, ret;
274  u8 tmp[4];
275 
276  if (!irq_base) {
277  dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
278  return -EINVAL;
279  }
280 
281  mutex_init(&tps6586x->irq_lock);
282  for (i = 0; i < 5; i++) {
283  tps6586x->mask_reg[i] = 0xff;
284  tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
285  }
286 
287  tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
288 
289  tps6586x->irq_base = irq_base;
290 
291  tps6586x->irq_chip.name = "tps6586x";
292  tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
293  tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
294  tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
295  tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
296 
297  for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
298  int __irq = i + tps6586x->irq_base;
299  irq_set_chip_data(__irq, tps6586x);
300  irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
302  irq_set_nested_thread(__irq, 1);
303 #ifdef CONFIG_ARM
304  set_irq_flags(__irq, IRQF_VALID);
305 #endif
306  }
307 
308  ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
309  "tps6586x", tps6586x);
310 
311  if (!ret) {
312  device_init_wakeup(tps6586x->dev, 1);
313  enable_irq_wake(irq);
314  }
315 
316  return ret;
317 }
318 
319 static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
321 {
323  struct platform_device *pdev;
324  int i, ret = 0;
325 
326  for (i = 0; i < pdata->num_subdevs; i++) {
327  subdev = &pdata->subdevs[i];
328 
329  pdev = platform_device_alloc(subdev->name, subdev->id);
330  if (!pdev) {
331  ret = -ENOMEM;
332  goto failed;
333  }
334 
335  pdev->dev.parent = tps6586x->dev;
336  pdev->dev.platform_data = subdev->platform_data;
337  pdev->dev.of_node = subdev->of_node;
338 
339  ret = platform_device_add(pdev);
340  if (ret) {
341  platform_device_put(pdev);
342  goto failed;
343  }
344  }
345  return 0;
346 
347 failed:
348  tps6586x_remove_subdevs(tps6586x);
349  return ret;
350 }
351 
352 #ifdef CONFIG_OF
353 static struct of_regulator_match tps6586x_matches[] = {
354  { .name = "sys", .driver_data = (void *)TPS6586X_ID_SYS },
355  { .name = "sm0", .driver_data = (void *)TPS6586X_ID_SM_0 },
356  { .name = "sm1", .driver_data = (void *)TPS6586X_ID_SM_1 },
357  { .name = "sm2", .driver_data = (void *)TPS6586X_ID_SM_2 },
358  { .name = "ldo0", .driver_data = (void *)TPS6586X_ID_LDO_0 },
359  { .name = "ldo1", .driver_data = (void *)TPS6586X_ID_LDO_1 },
360  { .name = "ldo2", .driver_data = (void *)TPS6586X_ID_LDO_2 },
361  { .name = "ldo3", .driver_data = (void *)TPS6586X_ID_LDO_3 },
362  { .name = "ldo4", .driver_data = (void *)TPS6586X_ID_LDO_4 },
363  { .name = "ldo5", .driver_data = (void *)TPS6586X_ID_LDO_5 },
364  { .name = "ldo6", .driver_data = (void *)TPS6586X_ID_LDO_6 },
365  { .name = "ldo7", .driver_data = (void *)TPS6586X_ID_LDO_7 },
366  { .name = "ldo8", .driver_data = (void *)TPS6586X_ID_LDO_8 },
367  { .name = "ldo9", .driver_data = (void *)TPS6586X_ID_LDO_9 },
368  { .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
369 };
370 
371 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
372 {
373  const unsigned int num = ARRAY_SIZE(tps6586x_matches);
374  struct device_node *np = client->dev.of_node;
376  struct tps6586x_subdev_info *devs;
377  struct device_node *regs;
378  const char *sys_rail_name = NULL;
379  unsigned int count;
380  unsigned int i, j;
381  int err;
382 
383  regs = of_find_node_by_name(np, "regulators");
384  if (!regs)
385  return NULL;
386 
387  err = of_regulator_match(&client->dev, regs, tps6586x_matches, num);
388  if (err < 0) {
389  of_node_put(regs);
390  return NULL;
391  }
392 
393  of_node_put(regs);
394  count = err;
395 
396  devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL);
397  if (!devs)
398  return NULL;
399 
400  for (i = 0, j = 0; i < num && j < count; i++) {
401  struct regulator_init_data *reg_idata;
402 
403  if (!tps6586x_matches[i].init_data)
404  continue;
405 
406  reg_idata = tps6586x_matches[i].init_data;
407  devs[j].name = "tps6586x-regulator";
408  devs[j].platform_data = tps6586x_matches[i].init_data;
409  devs[j].id = (int)tps6586x_matches[i].driver_data;
410  if (devs[j].id == TPS6586X_ID_SYS)
411  sys_rail_name = reg_idata->constraints.name;
412 
413  if ((devs[j].id == TPS6586X_ID_LDO_5) ||
414  (devs[j].id == TPS6586X_ID_LDO_RTC))
415  reg_idata->supply_regulator = sys_rail_name;
416 
417  devs[j].of_node = tps6586x_matches[i].of_node;
418  j++;
419  }
420 
421  pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
422  if (!pdata)
423  return NULL;
424 
425  pdata->num_subdevs = count;
426  pdata->subdevs = devs;
427  pdata->gpio_base = -1;
428  pdata->irq_base = -1;
429  pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
430 
431  return pdata;
432 }
433 
434 static struct of_device_id tps6586x_of_match[] = {
435  { .compatible = "ti,tps6586x", },
436  { },
437 };
438 #else
439 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
440 {
441  return NULL;
442 }
443 #endif
444 
445 static bool is_volatile_reg(struct device *dev, unsigned int reg)
446 {
447  /* Cache all interrupt mask register */
448  if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
449  return false;
450 
451  return true;
452 }
453 
454 static const struct regmap_config tps6586x_regmap_config = {
455  .reg_bits = 8,
456  .val_bits = 8,
457  .max_register = TPS6586X_MAX_REGISTER - 1,
458  .volatile_reg = is_volatile_reg,
459  .cache_type = REGCACHE_RBTREE,
460 };
461 
462 static struct device *tps6586x_dev;
463 static void tps6586x_power_off(void)
464 {
466  return;
467 
469 }
470 
471 static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
472  const struct i2c_device_id *id)
473 {
474  struct tps6586x_platform_data *pdata = client->dev.platform_data;
475  struct tps6586x *tps6586x;
476  int ret;
477 
478  if (!pdata && client->dev.of_node)
479  pdata = tps6586x_parse_dt(client);
480 
481  if (!pdata) {
482  dev_err(&client->dev, "tps6586x requires platform data\n");
483  return -ENOTSUPP;
484  }
485 
487  if (ret < 0) {
488  dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
489  return -EIO;
490  }
491 
492  dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
493 
494  tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
495  if (tps6586x == NULL) {
496  dev_err(&client->dev, "memory for tps6586x alloc failed\n");
497  return -ENOMEM;
498  }
499 
500  tps6586x->client = client;
501  tps6586x->dev = &client->dev;
502  i2c_set_clientdata(client, tps6586x);
503 
504  tps6586x->regmap = devm_regmap_init_i2c(client,
505  &tps6586x_regmap_config);
506  if (IS_ERR(tps6586x->regmap)) {
507  ret = PTR_ERR(tps6586x->regmap);
508  dev_err(&client->dev, "regmap init failed: %d\n", ret);
509  return ret;
510  }
511 
512 
513  if (client->irq) {
514  ret = tps6586x_irq_init(tps6586x, client->irq,
515  pdata->irq_base);
516  if (ret) {
517  dev_err(&client->dev, "IRQ init failed: %d\n", ret);
518  return ret;
519  }
520  }
521 
522  ret = mfd_add_devices(tps6586x->dev, -1,
523  tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
524  NULL, 0, NULL);
525  if (ret < 0) {
526  dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
527  goto err_mfd_add;
528  }
529 
530  ret = tps6586x_add_subdevs(tps6586x, pdata);
531  if (ret) {
532  dev_err(&client->dev, "add devices failed: %d\n", ret);
533  goto err_add_devs;
534  }
535 
536  if (pdata->pm_off && !pm_power_off) {
537  tps6586x_dev = &client->dev;
538  pm_power_off = tps6586x_power_off;
539  }
540 
541  return 0;
542 
543 err_add_devs:
544  mfd_remove_devices(tps6586x->dev);
545 err_mfd_add:
546  if (client->irq)
547  free_irq(client->irq, tps6586x);
548  return ret;
549 }
550 
551 static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
552 {
553  struct tps6586x *tps6586x = i2c_get_clientdata(client);
554 
555  tps6586x_remove_subdevs(tps6586x);
556  mfd_remove_devices(tps6586x->dev);
557  if (client->irq)
558  free_irq(client->irq, tps6586x);
559  return 0;
560 }
561 
562 static const struct i2c_device_id tps6586x_id_table[] = {
563  { "tps6586x", 0 },
564  { },
565 };
566 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
567 
568 static struct i2c_driver tps6586x_driver = {
569  .driver = {
570  .name = "tps6586x",
571  .owner = THIS_MODULE,
572  .of_match_table = of_match_ptr(tps6586x_of_match),
573  },
574  .probe = tps6586x_i2c_probe,
575  .remove = __devexit_p(tps6586x_i2c_remove),
576  .id_table = tps6586x_id_table,
577 };
578 
579 static int __init tps6586x_init(void)
580 {
581  return i2c_add_driver(&tps6586x_driver);
582 }
583 subsys_initcall(tps6586x_init);
584 
585 static void __exit tps6586x_exit(void)
586 {
587  i2c_del_driver(&tps6586x_driver);
588 }
589 module_exit(tps6586x_exit);
590 
591 MODULE_DESCRIPTION("TPS6586X core driver");
592 MODULE_AUTHOR("Mike Rapoport <[email protected]>");
593 MODULE_LICENSE("GPL");