onsemi-ncp6335d.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/log2.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/regmap.h>
  25. #include <linux/regulator/onsemi-ncp6335d.h>
  26. #include <linux/string.h>
  27. #include <mach/gpiomux.h>
  28. /* registers */
  29. #define REG_NCP6335D_PID 0x03
  30. #define REG_NCP6335D_PROGVSEL1 0x10
  31. #define REG_NCP6335D_PROGVSEL0 0x11
  32. #define REG_NCP6335D_PGOOD 0x12
  33. #define REG_NCP6335D_TIMING 0x13
  34. #define REG_NCP6335D_COMMAND 0x14
  35. /* constraints */
  36. #define NCP6335D_MIN_VOLTAGE_UV 600000
  37. #define NCP6335D_STEP_VOLTAGE_UV 6250
  38. #define NCP6335D_VOLTAGE_STEPS 128
  39. #define NCP6335D_MIN_SLEW_NS 166
  40. #define NCP6335D_MAX_SLEW_NS 1333
  41. /* bits */
  42. #define NCP6335D_ENABLE BIT(7)
  43. #define NCP6335D_DVS_PWM_MODE BIT(5)
  44. #define NCP6335D_PWM_MODE1 BIT(6)
  45. #define NCP6335D_PWM_MODE0 BIT(7)
  46. #define NCP6335D_PGOOD_DISCHG BIT(4)
  47. #define NCP6335D_SLEEP_MODE BIT(4)
  48. #define NCP6335D_VOUT_SEL_MASK 0x7F
  49. #define NCP6335D_SLEW_MASK 0x18
  50. #define NCP6335D_SLEW_SHIFT 0x3
  51. struct ncp6335d_info {
  52. struct regulator_dev *regulator;
  53. struct regulator_init_data *init_data;
  54. struct regmap *regmap;
  55. struct device *dev;
  56. unsigned int vsel_reg;
  57. unsigned int vsel_backup_reg;
  58. unsigned int mode_bit;
  59. int curr_voltage;
  60. int slew_rate;
  61. unsigned int step_size;
  62. unsigned int min_voltage;
  63. unsigned int min_slew_ns;
  64. unsigned int max_slew_ns;
  65. };
  66. static void dump_registers(struct ncp6335d_info *dd,
  67. unsigned int reg, const char *func)
  68. {
  69. unsigned int val = 0;
  70. regmap_read(dd->regmap, reg, &val);
  71. dev_dbg(dd->dev, "%s: NCP6335D: Reg = %x, Val = %x\n", func, reg, val);
  72. }
  73. static void ncp633d_slew_delay(struct ncp6335d_info *dd,
  74. int prev_uV, int new_uV)
  75. {
  76. u8 val;
  77. int delay;
  78. val = abs(prev_uV - new_uV) / dd->step_size;
  79. delay = ((val * dd->slew_rate) / 1000) + 1;
  80. dev_dbg(dd->dev, "Slew Delay = %d\n", delay);
  81. udelay(delay);
  82. }
  83. static int ncp6335d_enable(struct regulator_dev *rdev)
  84. {
  85. int rc;
  86. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  87. rc = regmap_update_bits(dd->regmap, dd->vsel_reg,
  88. NCP6335D_ENABLE, NCP6335D_ENABLE);
  89. if (rc)
  90. dev_err(dd->dev, "Unable to enable regualtor rc(%d)", rc);
  91. dump_registers(dd, dd->vsel_reg, __func__);
  92. return rc;
  93. }
  94. static int ncp6335d_disable(struct regulator_dev *rdev)
  95. {
  96. int rc;
  97. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  98. rc = regmap_update_bits(dd->regmap, dd->vsel_reg,
  99. NCP6335D_ENABLE, 0);
  100. if (rc)
  101. dev_err(dd->dev, "Unable to disable regualtor rc(%d)", rc);
  102. dump_registers(dd, dd->vsel_reg, __func__);
  103. return rc;
  104. }
  105. static int ncp6335d_get_voltage(struct regulator_dev *rdev)
  106. {
  107. unsigned int val;
  108. int rc;
  109. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  110. rc = regmap_read(dd->regmap, dd->vsel_reg, &val);
  111. if (rc) {
  112. dev_err(dd->dev, "Unable to get volatge rc(%d)", rc);
  113. return rc;
  114. }
  115. dd->curr_voltage = ((val & NCP6335D_VOUT_SEL_MASK) * dd->step_size) +
  116. dd->min_voltage;
  117. dump_registers(dd, dd->vsel_reg, __func__);
  118. return dd->curr_voltage;
  119. }
  120. static int ncp6335d_set_voltage(struct regulator_dev *rdev,
  121. int min_uV, int max_uV, unsigned *selector)
  122. {
  123. int rc, set_val, new_uV;
  124. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  125. set_val = DIV_ROUND_UP(min_uV - dd->min_voltage, dd->step_size);
  126. new_uV = (set_val * dd->step_size) + dd->min_voltage;
  127. if (new_uV > max_uV) {
  128. dev_err(dd->dev, "Unable to set volatge (%d %d)\n",
  129. min_uV, max_uV);
  130. return -EINVAL;
  131. }
  132. rc = regmap_update_bits(dd->regmap, dd->vsel_reg,
  133. NCP6335D_VOUT_SEL_MASK, (set_val & NCP6335D_VOUT_SEL_MASK));
  134. if (rc) {
  135. dev_err(dd->dev, "Unable to set volatge (%d %d)\n",
  136. min_uV, max_uV);
  137. } else {
  138. ncp633d_slew_delay(dd, dd->curr_voltage, new_uV);
  139. dd->curr_voltage = new_uV;
  140. }
  141. dump_registers(dd, dd->vsel_reg, __func__);
  142. return rc;
  143. }
  144. static int ncp6335d_list_voltage(struct regulator_dev *rdev,
  145. unsigned selector)
  146. {
  147. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  148. if (selector >= NCP6335D_VOLTAGE_STEPS)
  149. return 0;
  150. return selector * dd->step_size + dd->min_voltage;
  151. }
  152. static int ncp6335d_set_mode(struct regulator_dev *rdev,
  153. unsigned int mode)
  154. {
  155. int rc;
  156. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  157. /* only FAST and NORMAL mode types are supported */
  158. if (mode != REGULATOR_MODE_FAST && mode != REGULATOR_MODE_NORMAL) {
  159. dev_err(dd->dev, "Mode %d not supported\n", mode);
  160. return -EINVAL;
  161. }
  162. rc = regmap_update_bits(dd->regmap, REG_NCP6335D_COMMAND, dd->mode_bit,
  163. (mode == REGULATOR_MODE_FAST) ? dd->mode_bit : 0);
  164. if (rc) {
  165. dev_err(dd->dev, "Unable to set operating mode rc(%d)", rc);
  166. return rc;
  167. }
  168. rc = regmap_update_bits(dd->regmap, REG_NCP6335D_COMMAND,
  169. NCP6335D_DVS_PWM_MODE,
  170. (mode == REGULATOR_MODE_FAST) ?
  171. NCP6335D_DVS_PWM_MODE : 0);
  172. if (rc)
  173. dev_err(dd->dev, "Unable to set DVS trans. mode rc(%d)", rc);
  174. dump_registers(dd, REG_NCP6335D_COMMAND, __func__);
  175. return rc;
  176. }
  177. static unsigned int ncp6335d_get_mode(struct regulator_dev *rdev)
  178. {
  179. unsigned int val;
  180. int rc;
  181. struct ncp6335d_info *dd = rdev_get_drvdata(rdev);
  182. rc = regmap_read(dd->regmap, REG_NCP6335D_COMMAND, &val);
  183. if (rc) {
  184. dev_err(dd->dev, "Unable to get regulator mode rc(%d)\n", rc);
  185. return rc;
  186. }
  187. dump_registers(dd, REG_NCP6335D_COMMAND, __func__);
  188. if (val & dd->mode_bit)
  189. return REGULATOR_MODE_FAST;
  190. return REGULATOR_MODE_NORMAL;
  191. }
  192. static struct regulator_ops ncp6335d_ops = {
  193. .set_voltage = ncp6335d_set_voltage,
  194. .get_voltage = ncp6335d_get_voltage,
  195. .list_voltage = ncp6335d_list_voltage,
  196. .enable = ncp6335d_enable,
  197. .disable = ncp6335d_disable,
  198. .set_mode = ncp6335d_set_mode,
  199. .get_mode = ncp6335d_get_mode,
  200. };
  201. static struct regulator_desc rdesc = {
  202. .name = "ncp6335d",
  203. .owner = THIS_MODULE,
  204. .n_voltages = NCP6335D_VOLTAGE_STEPS,
  205. .ops = &ncp6335d_ops,
  206. };
  207. static int ncp6335d_restore_working_reg(struct device_node *node,
  208. struct ncp6335d_info *dd)
  209. {
  210. int ret;
  211. unsigned int val;
  212. /* Restore register from back up register */
  213. ret = regmap_read(dd->regmap, dd->vsel_backup_reg, &val);
  214. if (ret < 0) {
  215. dev_err(dd->dev, "Failed to get backup data from reg %d, ret = %d\n",
  216. dd->vsel_backup_reg, ret);
  217. return ret;
  218. }
  219. ret = regmap_update_bits(dd->regmap, dd->vsel_reg,
  220. NCP6335D_VOUT_SEL_MASK, val);
  221. if (ret < 0) {
  222. dev_err(dd->dev, "Failed to update working reg %d, ret = %d\n",
  223. dd->vsel_reg, ret);
  224. return ret;
  225. }
  226. return ret;
  227. }
  228. static int ncp6335d_parse_gpio(struct device_node *node,
  229. struct ncp6335d_info *dd)
  230. {
  231. int ret = 0, gpio;
  232. enum of_gpio_flags flags;
  233. if (!of_find_property(node, "onnn,vsel-gpio", NULL))
  234. return ret;
  235. /* Get GPIO connected to vsel and set its output */
  236. gpio = of_get_named_gpio_flags(node,
  237. "onnn,vsel-gpio", 0, &flags);
  238. if (!gpio_is_valid(gpio)) {
  239. if (gpio != -EPROBE_DEFER)
  240. dev_err(dd->dev, "Could not get vsel, ret = %d\n",
  241. gpio);
  242. return gpio;
  243. }
  244. ret = devm_gpio_request(dd->dev, gpio, "ncp6335d_vsel");
  245. if (ret) {
  246. dev_err(dd->dev, "Failed to obtain gpio %d ret = %d\n",
  247. gpio, ret);
  248. return ret;
  249. }
  250. ret = gpio_direction_output(gpio, flags & OF_GPIO_ACTIVE_LOW ? 0 : 1);
  251. if (ret) {
  252. dev_err(dd->dev, "Failed to set GPIO %d to: %s, ret = %d",
  253. gpio, flags & OF_GPIO_ACTIVE_LOW ?
  254. "GPIO_LOW" : "GPIO_HIGH", ret);
  255. return ret;
  256. }
  257. return ret;
  258. }
  259. static int ncp6335d_parse_tlmm(struct device_node *node,
  260. struct ncp6335d_info *dd)
  261. {
  262. int val, ret = 0;
  263. u32 tmp[2];
  264. if (!of_find_property(node, "onnn,tlmm-config", NULL))
  265. return ret;
  266. ret = of_property_read_u32_array(node, "onnn,tlmm-config", tmp, 2);
  267. if (ret) {
  268. dev_err(dd->dev, "onnn,tlmm-config is misconfigured, ret = %d",
  269. ret);
  270. return ret;
  271. }
  272. val = msm_tlmm_misc_reg_read(TLMM_SPARE_REG);
  273. val &= ~tmp[0];
  274. val |= tmp[1] & tmp[0];
  275. msm_tlmm_misc_reg_write(TLMM_SPARE_REG, val);
  276. return ret;
  277. }
  278. static int __devinit ncp6335d_init(struct i2c_client *client,
  279. struct ncp6335d_info *dd,
  280. const struct ncp6335d_platform_data *pdata)
  281. {
  282. int rc;
  283. unsigned int val;
  284. switch (pdata->default_vsel) {
  285. case NCP6335D_VSEL0:
  286. dd->vsel_reg = REG_NCP6335D_PROGVSEL0;
  287. dd->vsel_backup_reg = REG_NCP6335D_PROGVSEL1;
  288. dd->mode_bit = NCP6335D_PWM_MODE0;
  289. break;
  290. case NCP6335D_VSEL1:
  291. dd->vsel_reg = REG_NCP6335D_PROGVSEL1;
  292. dd->vsel_backup_reg = REG_NCP6335D_PROGVSEL0;
  293. dd->mode_bit = NCP6335D_PWM_MODE1;
  294. break;
  295. default:
  296. dev_err(dd->dev, "Invalid VSEL ID %d\n", pdata->default_vsel);
  297. return -EINVAL;
  298. }
  299. if (of_property_read_bool(client->dev.of_node, "onnn,restore-reg")) {
  300. rc = ncp6335d_restore_working_reg(client->dev.of_node, dd);
  301. if (rc)
  302. return rc;
  303. }
  304. rc = ncp6335d_parse_gpio(client->dev.of_node, dd);
  305. if (rc)
  306. return rc;
  307. rc = ncp6335d_parse_tlmm(client->dev.of_node, dd);
  308. if (rc)
  309. return rc;
  310. /* get the current programmed voltage */
  311. rc = regmap_read(dd->regmap, dd->vsel_reg, &val);
  312. if (rc) {
  313. dev_err(dd->dev, "Unable to get volatge rc(%d)", rc);
  314. return rc;
  315. }
  316. dd->curr_voltage = ((val & NCP6335D_VOUT_SEL_MASK) *
  317. dd->step_size) + dd->min_voltage;
  318. /* set discharge */
  319. rc = regmap_update_bits(dd->regmap, REG_NCP6335D_PGOOD,
  320. NCP6335D_PGOOD_DISCHG,
  321. (pdata->discharge_enable ?
  322. NCP6335D_PGOOD_DISCHG : 0));
  323. if (rc) {
  324. dev_err(dd->dev, "Unable to set Active Discharge rc(%d)\n", rc);
  325. return -EINVAL;
  326. }
  327. /* set slew rate */
  328. if (pdata->slew_rate_ns < dd->min_slew_ns ||
  329. pdata->slew_rate_ns > dd->max_slew_ns) {
  330. dev_err(dd->dev, "Invalid slew rate %d\n", pdata->slew_rate_ns);
  331. return -EINVAL;
  332. }
  333. dd->slew_rate = pdata->slew_rate_ns;
  334. val = DIV_ROUND_UP(pdata->slew_rate_ns, dd->min_slew_ns);
  335. val = ilog2(val);
  336. rc = regmap_update_bits(dd->regmap, REG_NCP6335D_TIMING,
  337. NCP6335D_SLEW_MASK, val << NCP6335D_SLEW_SHIFT);
  338. if (rc)
  339. dev_err(dd->dev, "Unable to set slew rate rc(%d)\n", rc);
  340. /* Set Sleep mode bit */
  341. rc = regmap_update_bits(dd->regmap, REG_NCP6335D_COMMAND,
  342. NCP6335D_SLEEP_MODE, pdata->sleep_enable ?
  343. NCP6335D_SLEEP_MODE : 0);
  344. if (rc)
  345. dev_err(dd->dev, "Unable to set sleep mode (%d)\n", rc);
  346. dump_registers(dd, REG_NCP6335D_COMMAND, __func__);
  347. dump_registers(dd, REG_NCP6335D_PROGVSEL0, __func__);
  348. dump_registers(dd, REG_NCP6335D_TIMING, __func__);
  349. dump_registers(dd, REG_NCP6335D_PGOOD, __func__);
  350. return rc;
  351. }
  352. static struct regmap_config ncp6335d_regmap_config = {
  353. .reg_bits = 8,
  354. .val_bits = 8,
  355. };
  356. static int ncp6335d_parse_dt(struct i2c_client *client,
  357. struct ncp6335d_info *dd)
  358. {
  359. int rc;
  360. rc = of_property_read_u32(client->dev.of_node,
  361. "onnn,step-size", &dd->step_size);
  362. if (rc < 0) {
  363. dev_err(&client->dev, "step size missing: rc = %d.\n", rc);
  364. return rc;
  365. }
  366. rc = of_property_read_u32(client->dev.of_node,
  367. "onnn,min-slew-ns", &dd->min_slew_ns);
  368. if (rc < 0) {
  369. dev_err(&client->dev, "min slew us missing: rc = %d.\n", rc);
  370. return rc;
  371. }
  372. rc = of_property_read_u32(client->dev.of_node,
  373. "onnn,max-slew-ns", &dd->max_slew_ns);
  374. if (rc < 0) {
  375. dev_err(&client->dev, "max slew us missing: rc = %d.\n", rc);
  376. return rc;
  377. }
  378. rc = of_property_read_u32(client->dev.of_node,
  379. "onnn,min-setpoint", &dd->min_voltage);
  380. if (rc < 0) {
  381. dev_err(&client->dev, "min set point missing: rc = %d.\n", rc);
  382. return rc;
  383. }
  384. return rc;
  385. }
  386. static struct ncp6335d_platform_data *
  387. ncp6335d_get_of_platform_data(struct i2c_client *client)
  388. {
  389. struct ncp6335d_platform_data *pdata = NULL;
  390. struct regulator_init_data *init_data;
  391. const char *mode_name;
  392. int rc;
  393. init_data = of_get_regulator_init_data(&client->dev,
  394. client->dev.of_node);
  395. if (!init_data) {
  396. dev_err(&client->dev, "regulator init data is missing\n");
  397. return pdata;
  398. }
  399. pdata = devm_kzalloc(&client->dev,
  400. sizeof(struct ncp6335d_platform_data), GFP_KERNEL);
  401. if (!pdata) {
  402. dev_err(&client->dev, "ncp6335d_platform_data allocation failed.\n");
  403. return pdata;
  404. }
  405. rc = of_property_read_u32(client->dev.of_node,
  406. "onnn,vsel", &pdata->default_vsel);
  407. if (rc < 0) {
  408. dev_err(&client->dev, "onnn,vsel property missing: rc = %d.\n",
  409. rc);
  410. return NULL;
  411. }
  412. rc = of_property_read_u32(client->dev.of_node,
  413. "onnn,slew-ns", &pdata->slew_rate_ns);
  414. if (rc < 0) {
  415. dev_err(&client->dev, "onnn,slew-ns property missing: rc = %d.\n",
  416. rc);
  417. return NULL;
  418. }
  419. pdata->discharge_enable = of_property_read_bool(client->dev.of_node,
  420. "onnn,discharge-enable");
  421. pdata->sleep_enable = of_property_read_bool(client->dev.of_node,
  422. "onnn,sleep-enable");
  423. pdata->init_data = init_data;
  424. init_data->constraints.input_uV = init_data->constraints.max_uV;
  425. init_data->constraints.valid_ops_mask =
  426. REGULATOR_CHANGE_VOLTAGE |
  427. REGULATOR_CHANGE_STATUS |
  428. REGULATOR_CHANGE_MODE;
  429. init_data->constraints.valid_modes_mask =
  430. REGULATOR_MODE_NORMAL |
  431. REGULATOR_MODE_FAST;
  432. rc = of_property_read_string(client->dev.of_node, "onnn,mode",
  433. &mode_name);
  434. if (!rc) {
  435. if (strcmp("pwm", mode_name) == 0) {
  436. init_data->constraints.initial_mode =
  437. REGULATOR_MODE_FAST;
  438. } else if (strcmp("auto", mode_name) == 0) {
  439. init_data->constraints.initial_mode =
  440. REGULATOR_MODE_NORMAL;
  441. } else {
  442. dev_err(&client->dev, "onnn,mode, unknown regulator mode: %s\n",
  443. mode_name);
  444. return NULL;
  445. }
  446. }
  447. return pdata;
  448. }
  449. static int __devinit ncp6335d_regulator_probe(struct i2c_client *client,
  450. const struct i2c_device_id *id)
  451. {
  452. int rc;
  453. unsigned int val = 0;
  454. struct ncp6335d_info *dd;
  455. const struct ncp6335d_platform_data *pdata;
  456. if (client->dev.of_node)
  457. pdata = ncp6335d_get_of_platform_data(client);
  458. else
  459. pdata = client->dev.platform_data;
  460. if (!pdata) {
  461. dev_err(&client->dev, "Platform data not specified\n");
  462. return -EINVAL;
  463. }
  464. dd = devm_kzalloc(&client->dev, sizeof(*dd), GFP_KERNEL);
  465. if (!dd) {
  466. dev_err(&client->dev, "Unable to allocate memory\n");
  467. return -ENOMEM;
  468. }
  469. if (client->dev.of_node) {
  470. rc = ncp6335d_parse_dt(client, dd);
  471. if (rc)
  472. return rc;
  473. } else {
  474. dd->step_size = NCP6335D_STEP_VOLTAGE_UV;
  475. dd->min_voltage = NCP6335D_MIN_VOLTAGE_UV;
  476. dd->min_slew_ns = NCP6335D_MIN_SLEW_NS;
  477. dd->max_slew_ns = NCP6335D_MAX_SLEW_NS;
  478. }
  479. dd->regmap = devm_regmap_init_i2c(client, &ncp6335d_regmap_config);
  480. if (IS_ERR(dd->regmap)) {
  481. dev_err(&client->dev, "Error allocating regmap\n");
  482. return PTR_ERR(dd->regmap);
  483. }
  484. rc = regmap_read(dd->regmap, REG_NCP6335D_PID, &val);
  485. if (rc) {
  486. dev_err(&client->dev, "Unable to identify NCP6335D, rc(%d)\n",
  487. rc);
  488. return rc;
  489. }
  490. dev_info(&client->dev, "Detected Regulator NCP6335D PID = %d\n", val);
  491. dd->init_data = pdata->init_data;
  492. dd->dev = &client->dev;
  493. i2c_set_clientdata(client, dd);
  494. rc = ncp6335d_init(client, dd, pdata);
  495. if (rc) {
  496. dev_err(&client->dev, "Unable to intialize the regulator\n");
  497. return -EINVAL;
  498. }
  499. dd->regulator = regulator_register(&rdesc, &client->dev, dd->init_data,
  500. dd, client->dev.of_node);
  501. if (IS_ERR(dd->regulator)) {
  502. dev_err(&client->dev, "Unable to register regulator rc(%ld)",
  503. PTR_ERR(dd->regulator));
  504. return PTR_ERR(dd->regulator);
  505. }
  506. return 0;
  507. }
  508. static int __devexit ncp6335d_regulator_remove(struct i2c_client *client)
  509. {
  510. struct ncp6335d_info *dd = i2c_get_clientdata(client);
  511. regulator_unregister(dd->regulator);
  512. return 0;
  513. }
  514. static struct of_device_id ncp6335d_match_table[] = {
  515. { .compatible = "onnn,ncp6335d-regulator", },
  516. {},
  517. };
  518. MODULE_DEVICE_TABLE(of, ncp6335d_match_table);
  519. static const struct i2c_device_id ncp6335d_id[] = {
  520. {"ncp6335d", -1},
  521. { },
  522. };
  523. static struct i2c_driver ncp6335d_regulator_driver = {
  524. .driver = {
  525. .name = "ncp6335d-regulator",
  526. .owner = THIS_MODULE,
  527. .of_match_table = ncp6335d_match_table,
  528. },
  529. .probe = ncp6335d_regulator_probe,
  530. .remove = __devexit_p(ncp6335d_regulator_remove),
  531. .id_table = ncp6335d_id,
  532. };
  533. /**
  534. * ncp6335d_regulator_init() - initialized ncp6335d regulator driver
  535. * This function registers the ncp6335d regulator platform driver.
  536. *
  537. * Returns 0 on success or errno on failure.
  538. */
  539. int __init ncp6335d_regulator_init(void)
  540. {
  541. static bool initialized;
  542. if (initialized)
  543. return 0;
  544. else
  545. initialized = true;
  546. return i2c_add_driver(&ncp6335d_regulator_driver);
  547. }
  548. EXPORT_SYMBOL(ncp6335d_regulator_init);
  549. subsys_initcall(ncp6335d_regulator_init);
  550. static void __exit ncp6335d_regulator_exit(void)
  551. {
  552. i2c_del_driver(&ncp6335d_regulator_driver);
  553. }
  554. module_exit(ncp6335d_regulator_exit);
  555. MODULE_DESCRIPTION("OnSemi-NCP6335D regulator driver");
  556. MODULE_LICENSE("GPL v2");