tps6524x-regulator.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Regulator driver for TPS6524x PMIC
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  11. * whether express or implied; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #define REG_LDO_SET 0x0
  24. #define LDO_ILIM_MASK 1 /* 0 = 400-800, 1 = 900-1500 */
  25. #define LDO_VSEL_MASK 0x0f
  26. #define LDO2_ILIM_SHIFT 12
  27. #define LDO2_VSEL_SHIFT 4
  28. #define LDO1_ILIM_SHIFT 8
  29. #define LDO1_VSEL_SHIFT 0
  30. #define REG_BLOCK_EN 0x1
  31. #define BLOCK_MASK 1
  32. #define BLOCK_LDO1_SHIFT 0
  33. #define BLOCK_LDO2_SHIFT 1
  34. #define BLOCK_LCD_SHIFT 2
  35. #define BLOCK_USB_SHIFT 3
  36. #define REG_DCDC_SET 0x2
  37. #define DCDC_VDCDC_MASK 0x1f
  38. #define DCDC_VDCDC1_SHIFT 0
  39. #define DCDC_VDCDC2_SHIFT 5
  40. #define DCDC_VDCDC3_SHIFT 10
  41. #define REG_DCDC_EN 0x3
  42. #define DCDCDCDC_EN_MASK 0x1
  43. #define DCDCDCDC1_EN_SHIFT 0
  44. #define DCDCDCDC1_PG_MSK BIT(1)
  45. #define DCDCDCDC2_EN_SHIFT 2
  46. #define DCDCDCDC2_PG_MSK BIT(3)
  47. #define DCDCDCDC3_EN_SHIFT 4
  48. #define DCDCDCDC3_PG_MSK BIT(5)
  49. #define REG_USB 0x4
  50. #define USB_ILIM_SHIFT 0
  51. #define USB_ILIM_MASK 0x3
  52. #define USB_TSD_SHIFT 2
  53. #define USB_TSD_MASK 0x3
  54. #define USB_TWARN_SHIFT 4
  55. #define USB_TWARN_MASK 0x3
  56. #define USB_IWARN_SD BIT(6)
  57. #define USB_FAST_LOOP BIT(7)
  58. #define REG_ALARM 0x5
  59. #define ALARM_LDO1 BIT(0)
  60. #define ALARM_DCDC1 BIT(1)
  61. #define ALARM_DCDC2 BIT(2)
  62. #define ALARM_DCDC3 BIT(3)
  63. #define ALARM_LDO2 BIT(4)
  64. #define ALARM_USB_WARN BIT(5)
  65. #define ALARM_USB_ALARM BIT(6)
  66. #define ALARM_LCD BIT(9)
  67. #define ALARM_TEMP_WARM BIT(10)
  68. #define ALARM_TEMP_HOT BIT(11)
  69. #define ALARM_NRST BIT(14)
  70. #define ALARM_POWERUP BIT(15)
  71. #define REG_INT_ENABLE 0x6
  72. #define INT_LDO1 BIT(0)
  73. #define INT_DCDC1 BIT(1)
  74. #define INT_DCDC2 BIT(2)
  75. #define INT_DCDC3 BIT(3)
  76. #define INT_LDO2 BIT(4)
  77. #define INT_USB_WARN BIT(5)
  78. #define INT_USB_ALARM BIT(6)
  79. #define INT_LCD BIT(9)
  80. #define INT_TEMP_WARM BIT(10)
  81. #define INT_TEMP_HOT BIT(11)
  82. #define INT_GLOBAL_EN BIT(15)
  83. #define REG_INT_STATUS 0x7
  84. #define STATUS_LDO1 BIT(0)
  85. #define STATUS_DCDC1 BIT(1)
  86. #define STATUS_DCDC2 BIT(2)
  87. #define STATUS_DCDC3 BIT(3)
  88. #define STATUS_LDO2 BIT(4)
  89. #define STATUS_USB_WARN BIT(5)
  90. #define STATUS_USB_ALARM BIT(6)
  91. #define STATUS_LCD BIT(9)
  92. #define STATUS_TEMP_WARM BIT(10)
  93. #define STATUS_TEMP_HOT BIT(11)
  94. #define REG_SOFTWARE_RESET 0xb
  95. #define REG_WRITE_ENABLE 0xd
  96. #define REG_REV_ID 0xf
  97. #define N_DCDC 3
  98. #define N_LDO 2
  99. #define N_SWITCH 2
  100. #define N_REGULATORS (3 /* DCDC */ + \
  101. 2 /* LDO */ + \
  102. 2 /* switch */)
  103. #define FIXED_ILIMSEL BIT(0)
  104. #define FIXED_VOLTAGE BIT(1)
  105. #define CMD_READ(reg) ((reg) << 6)
  106. #define CMD_WRITE(reg) (BIT(5) | (reg) << 6)
  107. #define STAT_CLK BIT(3)
  108. #define STAT_WRITE BIT(2)
  109. #define STAT_INVALID BIT(1)
  110. #define STAT_WP BIT(0)
  111. struct field {
  112. int reg;
  113. int shift;
  114. int mask;
  115. };
  116. struct supply_info {
  117. const char *name;
  118. int n_voltages;
  119. const int *voltages;
  120. int fixed_voltage;
  121. int n_ilimsels;
  122. const int *ilimsels;
  123. int fixed_ilimsel;
  124. int flags;
  125. struct field enable, voltage, ilimsel;
  126. };
  127. struct tps6524x {
  128. struct device *dev;
  129. struct spi_device *spi;
  130. struct mutex lock;
  131. struct regulator_desc desc[N_REGULATORS];
  132. struct regulator_dev *rdev[N_REGULATORS];
  133. };
  134. static int __read_reg(struct tps6524x *hw, int reg)
  135. {
  136. int error = 0;
  137. u16 cmd = CMD_READ(reg), in;
  138. u8 status;
  139. struct spi_message m;
  140. struct spi_transfer t[3];
  141. spi_message_init(&m);
  142. memset(t, 0, sizeof(t));
  143. t[0].tx_buf = &cmd;
  144. t[0].len = 2;
  145. t[0].bits_per_word = 12;
  146. spi_message_add_tail(&t[0], &m);
  147. t[1].rx_buf = &in;
  148. t[1].len = 2;
  149. t[1].bits_per_word = 16;
  150. spi_message_add_tail(&t[1], &m);
  151. t[2].rx_buf = &status;
  152. t[2].len = 1;
  153. t[2].bits_per_word = 4;
  154. spi_message_add_tail(&t[2], &m);
  155. error = spi_sync(hw->spi, &m);
  156. if (error < 0)
  157. return error;
  158. dev_dbg(hw->dev, "read reg %d, data %x, status %x\n",
  159. reg, in, status);
  160. if (!(status & STAT_CLK) || (status & STAT_WRITE))
  161. return -EIO;
  162. if (status & STAT_INVALID)
  163. return -EINVAL;
  164. return in;
  165. }
  166. static int read_reg(struct tps6524x *hw, int reg)
  167. {
  168. int ret;
  169. mutex_lock(&hw->lock);
  170. ret = __read_reg(hw, reg);
  171. mutex_unlock(&hw->lock);
  172. return ret;
  173. }
  174. static int __write_reg(struct tps6524x *hw, int reg, int val)
  175. {
  176. int error = 0;
  177. u16 cmd = CMD_WRITE(reg), out = val;
  178. u8 status;
  179. struct spi_message m;
  180. struct spi_transfer t[3];
  181. spi_message_init(&m);
  182. memset(t, 0, sizeof(t));
  183. t[0].tx_buf = &cmd;
  184. t[0].len = 2;
  185. t[0].bits_per_word = 12;
  186. spi_message_add_tail(&t[0], &m);
  187. t[1].tx_buf = &out;
  188. t[1].len = 2;
  189. t[1].bits_per_word = 16;
  190. spi_message_add_tail(&t[1], &m);
  191. t[2].rx_buf = &status;
  192. t[2].len = 1;
  193. t[2].bits_per_word = 4;
  194. spi_message_add_tail(&t[2], &m);
  195. error = spi_sync(hw->spi, &m);
  196. if (error < 0)
  197. return error;
  198. dev_dbg(hw->dev, "wrote reg %d, data %x, status %x\n",
  199. reg, out, status);
  200. if (!(status & STAT_CLK) || !(status & STAT_WRITE))
  201. return -EIO;
  202. if (status & (STAT_INVALID | STAT_WP))
  203. return -EINVAL;
  204. return error;
  205. }
  206. static int __rmw_reg(struct tps6524x *hw, int reg, int mask, int val)
  207. {
  208. int ret;
  209. ret = __read_reg(hw, reg);
  210. if (ret < 0)
  211. return ret;
  212. ret &= ~mask;
  213. ret |= val;
  214. ret = __write_reg(hw, reg, ret);
  215. return (ret < 0) ? ret : 0;
  216. }
  217. static int rmw_protect(struct tps6524x *hw, int reg, int mask, int val)
  218. {
  219. int ret;
  220. mutex_lock(&hw->lock);
  221. ret = __write_reg(hw, REG_WRITE_ENABLE, 1);
  222. if (ret) {
  223. dev_err(hw->dev, "failed to set write enable\n");
  224. goto error;
  225. }
  226. ret = __rmw_reg(hw, reg, mask, val);
  227. if (ret)
  228. dev_err(hw->dev, "failed to rmw register %d\n", reg);
  229. ret = __write_reg(hw, REG_WRITE_ENABLE, 0);
  230. if (ret) {
  231. dev_err(hw->dev, "failed to clear write enable\n");
  232. goto error;
  233. }
  234. error:
  235. mutex_unlock(&hw->lock);
  236. return ret;
  237. }
  238. static int read_field(struct tps6524x *hw, const struct field *field)
  239. {
  240. int tmp;
  241. tmp = read_reg(hw, field->reg);
  242. if (tmp < 0)
  243. return tmp;
  244. return (tmp >> field->shift) & field->mask;
  245. }
  246. static int write_field(struct tps6524x *hw, const struct field *field,
  247. int val)
  248. {
  249. if (val & ~field->mask)
  250. return -EOVERFLOW;
  251. return rmw_protect(hw, field->reg,
  252. field->mask << field->shift,
  253. val << field->shift);
  254. }
  255. static const int dcdc1_voltages[] = {
  256. 800000, 825000, 850000, 875000,
  257. 900000, 925000, 950000, 975000,
  258. 1000000, 1025000, 1050000, 1075000,
  259. 1100000, 1125000, 1150000, 1175000,
  260. 1200000, 1225000, 1250000, 1275000,
  261. 1300000, 1325000, 1350000, 1375000,
  262. 1400000, 1425000, 1450000, 1475000,
  263. 1500000, 1525000, 1550000, 1575000,
  264. };
  265. static const int dcdc2_voltages[] = {
  266. 1400000, 1450000, 1500000, 1550000,
  267. 1600000, 1650000, 1700000, 1750000,
  268. 1800000, 1850000, 1900000, 1950000,
  269. 2000000, 2050000, 2100000, 2150000,
  270. 2200000, 2250000, 2300000, 2350000,
  271. 2400000, 2450000, 2500000, 2550000,
  272. 2600000, 2650000, 2700000, 2750000,
  273. 2800000, 2850000, 2900000, 2950000,
  274. };
  275. static const int dcdc3_voltages[] = {
  276. 2400000, 2450000, 2500000, 2550000, 2600000,
  277. 2650000, 2700000, 2750000, 2800000, 2850000,
  278. 2900000, 2950000, 3000000, 3050000, 3100000,
  279. 3150000, 3200000, 3250000, 3300000, 3350000,
  280. 3400000, 3450000, 3500000, 3550000, 3600000,
  281. };
  282. static const int ldo1_voltages[] = {
  283. 4300000, 4350000, 4400000, 4450000,
  284. 4500000, 4550000, 4600000, 4650000,
  285. 4700000, 4750000, 4800000, 4850000,
  286. 4900000, 4950000, 5000000, 5050000,
  287. };
  288. static const int ldo2_voltages[] = {
  289. 1100000, 1150000, 1200000, 1250000,
  290. 1300000, 1700000, 1750000, 1800000,
  291. 1850000, 1900000, 3150000, 3200000,
  292. 3250000, 3300000, 3350000, 3400000,
  293. };
  294. static const int ldo_ilimsel[] = {
  295. 400000, 1500000
  296. };
  297. static const int usb_ilimsel[] = {
  298. 200000, 400000, 800000, 1000000
  299. };
  300. #define __MK_FIELD(_reg, _mask, _shift) \
  301. { .reg = (_reg), .mask = (_mask), .shift = (_shift), }
  302. static const struct supply_info supply_info[N_REGULATORS] = {
  303. {
  304. .name = "DCDC1",
  305. .flags = FIXED_ILIMSEL,
  306. .n_voltages = ARRAY_SIZE(dcdc1_voltages),
  307. .voltages = dcdc1_voltages,
  308. .fixed_ilimsel = 2400000,
  309. .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
  310. DCDCDCDC1_EN_SHIFT),
  311. .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
  312. DCDC_VDCDC1_SHIFT),
  313. },
  314. {
  315. .name = "DCDC2",
  316. .flags = FIXED_ILIMSEL,
  317. .n_voltages = ARRAY_SIZE(dcdc2_voltages),
  318. .voltages = dcdc2_voltages,
  319. .fixed_ilimsel = 1200000,
  320. .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
  321. DCDCDCDC2_EN_SHIFT),
  322. .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
  323. DCDC_VDCDC2_SHIFT),
  324. },
  325. {
  326. .name = "DCDC3",
  327. .flags = FIXED_ILIMSEL,
  328. .n_voltages = ARRAY_SIZE(dcdc3_voltages),
  329. .voltages = dcdc3_voltages,
  330. .fixed_ilimsel = 1200000,
  331. .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
  332. DCDCDCDC3_EN_SHIFT),
  333. .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
  334. DCDC_VDCDC3_SHIFT),
  335. },
  336. {
  337. .name = "LDO1",
  338. .n_voltages = ARRAY_SIZE(ldo1_voltages),
  339. .voltages = ldo1_voltages,
  340. .n_ilimsels = ARRAY_SIZE(ldo_ilimsel),
  341. .ilimsels = ldo_ilimsel,
  342. .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
  343. BLOCK_LDO1_SHIFT),
  344. .voltage = __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
  345. LDO1_VSEL_SHIFT),
  346. .ilimsel = __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
  347. LDO1_ILIM_SHIFT),
  348. },
  349. {
  350. .name = "LDO2",
  351. .n_voltages = ARRAY_SIZE(ldo2_voltages),
  352. .voltages = ldo2_voltages,
  353. .n_ilimsels = ARRAY_SIZE(ldo_ilimsel),
  354. .ilimsels = ldo_ilimsel,
  355. .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
  356. BLOCK_LDO2_SHIFT),
  357. .voltage = __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
  358. LDO2_VSEL_SHIFT),
  359. .ilimsel = __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
  360. LDO2_ILIM_SHIFT),
  361. },
  362. {
  363. .name = "USB",
  364. .flags = FIXED_VOLTAGE,
  365. .fixed_voltage = 5000000,
  366. .n_ilimsels = ARRAY_SIZE(usb_ilimsel),
  367. .ilimsels = usb_ilimsel,
  368. .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
  369. BLOCK_USB_SHIFT),
  370. .ilimsel = __MK_FIELD(REG_USB, USB_ILIM_MASK,
  371. USB_ILIM_SHIFT),
  372. },
  373. {
  374. .name = "LCD",
  375. .flags = FIXED_VOLTAGE | FIXED_ILIMSEL,
  376. .fixed_voltage = 5000000,
  377. .fixed_ilimsel = 400000,
  378. .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
  379. BLOCK_LCD_SHIFT),
  380. },
  381. };
  382. static int list_voltage(struct regulator_dev *rdev, unsigned selector)
  383. {
  384. const struct supply_info *info;
  385. struct tps6524x *hw;
  386. hw = rdev_get_drvdata(rdev);
  387. info = &supply_info[rdev_get_id(rdev)];
  388. if (info->flags & FIXED_VOLTAGE)
  389. return selector ? -EINVAL : info->fixed_voltage;
  390. return ((selector < info->n_voltages) ?
  391. info->voltages[selector] : -EINVAL);
  392. }
  393. static int set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
  394. unsigned *selector)
  395. {
  396. const struct supply_info *info;
  397. struct tps6524x *hw;
  398. unsigned i;
  399. hw = rdev_get_drvdata(rdev);
  400. info = &supply_info[rdev_get_id(rdev)];
  401. if (info->flags & FIXED_VOLTAGE)
  402. return -EINVAL;
  403. for (i = 0; i < info->n_voltages; i++)
  404. if (min_uV <= info->voltages[i] &&
  405. max_uV >= info->voltages[i])
  406. break;
  407. if (i >= info->n_voltages)
  408. i = info->n_voltages - 1;
  409. *selector = i;
  410. return write_field(hw, &info->voltage, i);
  411. }
  412. static int get_voltage(struct regulator_dev *rdev)
  413. {
  414. const struct supply_info *info;
  415. struct tps6524x *hw;
  416. int ret;
  417. hw = rdev_get_drvdata(rdev);
  418. info = &supply_info[rdev_get_id(rdev)];
  419. if (info->flags & FIXED_VOLTAGE)
  420. return info->fixed_voltage;
  421. ret = read_field(hw, &info->voltage);
  422. if (ret < 0)
  423. return ret;
  424. if (WARN_ON(ret >= info->n_voltages))
  425. return -EIO;
  426. return info->voltages[ret];
  427. }
  428. static int set_current_limit(struct regulator_dev *rdev, int min_uA,
  429. int max_uA)
  430. {
  431. const struct supply_info *info;
  432. struct tps6524x *hw;
  433. int i;
  434. hw = rdev_get_drvdata(rdev);
  435. info = &supply_info[rdev_get_id(rdev)];
  436. if (info->flags & FIXED_ILIMSEL)
  437. return -EINVAL;
  438. for (i = 0; i < info->n_ilimsels; i++)
  439. if (min_uA <= info->ilimsels[i] &&
  440. max_uA >= info->ilimsels[i])
  441. break;
  442. if (i >= info->n_ilimsels)
  443. return -EINVAL;
  444. return write_field(hw, &info->ilimsel, i);
  445. }
  446. static int get_current_limit(struct regulator_dev *rdev)
  447. {
  448. const struct supply_info *info;
  449. struct tps6524x *hw;
  450. int ret;
  451. hw = rdev_get_drvdata(rdev);
  452. info = &supply_info[rdev_get_id(rdev)];
  453. if (info->flags & FIXED_ILIMSEL)
  454. return info->fixed_ilimsel;
  455. ret = read_field(hw, &info->ilimsel);
  456. if (ret < 0)
  457. return ret;
  458. if (WARN_ON(ret >= info->n_ilimsels))
  459. return -EIO;
  460. return info->ilimsels[ret];
  461. }
  462. static int enable_supply(struct regulator_dev *rdev)
  463. {
  464. const struct supply_info *info;
  465. struct tps6524x *hw;
  466. hw = rdev_get_drvdata(rdev);
  467. info = &supply_info[rdev_get_id(rdev)];
  468. return write_field(hw, &info->enable, 1);
  469. }
  470. static int disable_supply(struct regulator_dev *rdev)
  471. {
  472. const struct supply_info *info;
  473. struct tps6524x *hw;
  474. hw = rdev_get_drvdata(rdev);
  475. info = &supply_info[rdev_get_id(rdev)];
  476. return write_field(hw, &info->enable, 0);
  477. }
  478. static int is_supply_enabled(struct regulator_dev *rdev)
  479. {
  480. const struct supply_info *info;
  481. struct tps6524x *hw;
  482. hw = rdev_get_drvdata(rdev);
  483. info = &supply_info[rdev_get_id(rdev)];
  484. return read_field(hw, &info->enable);
  485. }
  486. static struct regulator_ops regulator_ops = {
  487. .is_enabled = is_supply_enabled,
  488. .enable = enable_supply,
  489. .disable = disable_supply,
  490. .get_voltage = get_voltage,
  491. .set_voltage = set_voltage,
  492. .list_voltage = list_voltage,
  493. .set_current_limit = set_current_limit,
  494. .get_current_limit = get_current_limit,
  495. };
  496. static int pmic_remove(struct spi_device *spi)
  497. {
  498. struct tps6524x *hw = spi_get_drvdata(spi);
  499. int i;
  500. if (!hw)
  501. return 0;
  502. for (i = 0; i < N_REGULATORS; i++) {
  503. if (hw->rdev[i])
  504. regulator_unregister(hw->rdev[i]);
  505. hw->rdev[i] = NULL;
  506. }
  507. spi_set_drvdata(spi, NULL);
  508. kfree(hw);
  509. return 0;
  510. }
  511. static int __devinit pmic_probe(struct spi_device *spi)
  512. {
  513. struct tps6524x *hw;
  514. struct device *dev = &spi->dev;
  515. const struct supply_info *info = supply_info;
  516. struct regulator_init_data *init_data;
  517. int ret = 0, i;
  518. init_data = dev->platform_data;
  519. if (!init_data) {
  520. dev_err(dev, "could not find regulator platform data\n");
  521. return -EINVAL;
  522. }
  523. hw = kzalloc(sizeof(struct tps6524x), GFP_KERNEL);
  524. if (!hw) {
  525. dev_err(dev, "cannot allocate regulator private data\n");
  526. return -ENOMEM;
  527. }
  528. spi_set_drvdata(spi, hw);
  529. memset(hw, 0, sizeof(struct tps6524x));
  530. hw->dev = dev;
  531. hw->spi = spi_dev_get(spi);
  532. mutex_init(&hw->lock);
  533. for (i = 0; i < N_REGULATORS; i++, info++, init_data++) {
  534. hw->desc[i].name = info->name;
  535. hw->desc[i].id = i;
  536. hw->desc[i].n_voltages = info->n_voltages;
  537. hw->desc[i].ops = &regulator_ops;
  538. hw->desc[i].type = REGULATOR_VOLTAGE;
  539. hw->desc[i].owner = THIS_MODULE;
  540. if (info->flags & FIXED_VOLTAGE)
  541. hw->desc[i].n_voltages = 1;
  542. hw->rdev[i] = regulator_register(&hw->desc[i], dev,
  543. init_data, hw);
  544. if (IS_ERR(hw->rdev[i])) {
  545. ret = PTR_ERR(hw->rdev[i]);
  546. hw->rdev[i] = NULL;
  547. goto fail;
  548. }
  549. }
  550. return 0;
  551. fail:
  552. pmic_remove(spi);
  553. return ret;
  554. }
  555. static struct spi_driver pmic_driver = {
  556. .probe = pmic_probe,
  557. .remove = __devexit_p(pmic_remove),
  558. .driver = {
  559. .name = "tps6524x",
  560. .owner = THIS_MODULE,
  561. },
  562. };
  563. static int __init pmic_driver_init(void)
  564. {
  565. return spi_register_driver(&pmic_driver);
  566. }
  567. module_init(pmic_driver_init);
  568. static void __exit pmic_driver_exit(void)
  569. {
  570. spi_unregister_driver(&pmic_driver);
  571. }
  572. module_exit(pmic_driver_exit);
  573. MODULE_DESCRIPTION("TPS6524X PMIC Driver");
  574. MODULE_AUTHOR("Cyril Chemparathy");
  575. MODULE_LICENSE("GPL");
  576. MODULE_ALIAS("spi:tps6524x");