max77804k.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * max77804k.c - mfd core driver for the Maxim 77804k
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * SangYoung Son <hello.son@smasung.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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * This driver is based on max8997.c
  22. */
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/mutex.h>
  28. #include <linux/mfd/core.h>
  29. #include <linux/mfd/max77804k.h>
  30. #include <linux/mfd/max77804k-private.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/mfd/pm8xxx/misc.h>
  33. #if defined (CONFIG_OF)
  34. #include <linux/of_device.h>
  35. #include <linux/of_gpio.h>
  36. #endif
  37. #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
  38. #define I2C_ADDR_MUIC (0x4A >> 1)
  39. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  40. static struct mfd_cell max77804k_devs[] = {
  41. { .name = "max77804k-charger", },
  42. { .name = "max77804k-led", },
  43. { .name = "max77804k-muic", },
  44. { .name = "max77804k-safeout", },
  45. { .name = "max77804k-haptic", },
  46. };
  47. #if defined(CONFIG_EXTCON)
  48. struct max77804k_muic_data max77804k_muic = {
  49. .usb_sel = 0,
  50. .uart_sel = 0,
  51. };
  52. #endif
  53. int max77804k_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  54. {
  55. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  56. int ret;
  57. mutex_lock(&max77804k->iolock);
  58. ret = i2c_smbus_read_byte_data(i2c, reg);
  59. mutex_unlock(&max77804k->iolock);
  60. if (ret < 0) {
  61. dev_err(max77804k->dev,
  62. "%s, reg(0x%x), ret(%d)\n", __func__, reg, ret);
  63. return ret;
  64. }
  65. ret &= 0xff;
  66. *dest = ret;
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(max77804k_read_reg);
  70. int max77804k_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  71. {
  72. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  73. int ret;
  74. mutex_lock(&max77804k->iolock);
  75. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  76. mutex_unlock(&max77804k->iolock);
  77. if (ret < 0)
  78. return ret;
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(max77804k_bulk_read);
  82. int max77804k_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  83. {
  84. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  85. int ret;
  86. mutex_lock(&max77804k->iolock);
  87. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  88. if (ret < 0)
  89. dev_err(max77804k->dev,
  90. "%s, reg(0x%x), ret(%d)\n", __func__, reg, ret);
  91. mutex_unlock(&max77804k->iolock);
  92. return ret;
  93. }
  94. EXPORT_SYMBOL_GPL(max77804k_write_reg);
  95. int max77804k_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  96. {
  97. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  98. int ret;
  99. mutex_lock(&max77804k->iolock);
  100. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  101. mutex_unlock(&max77804k->iolock);
  102. if (ret < 0)
  103. return ret;
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(max77804k_bulk_write);
  107. int max77804k_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  108. {
  109. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  110. int ret;
  111. mutex_lock(&max77804k->iolock);
  112. ret = i2c_smbus_read_byte_data(i2c, reg);
  113. if (ret >= 0) {
  114. u8 old_val = ret & 0xff;
  115. u8 new_val = (val & mask) | (old_val & (~mask));
  116. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  117. }
  118. mutex_unlock(&max77804k->iolock);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(max77804k_update_reg);
  122. static int of_max77804k_dt(struct device *dev, struct max77804k_platform_data *pdata)
  123. {
  124. struct device_node *np = dev->of_node;
  125. int retval = 0;
  126. #ifdef CONFIG_SS_VIBRATOR
  127. struct max77804k_haptic_platform_data *haptic_data;
  128. haptic_data = kzalloc(sizeof(struct max77804k_haptic_platform_data), GFP_KERNEL);
  129. if (haptic_data == NULL)
  130. return -ENOMEM;
  131. if(!np) {
  132. kfree(haptic_data);
  133. return -EINVAL;
  134. }
  135. #endif
  136. pdata->irq_gpio = of_get_named_gpio_flags(np, "max77804k,irq-gpio",
  137. 0, &pdata->irq_gpio_flags);
  138. pdata->irq_base = irq_alloc_descs(-1, 0, MAX77804K_IRQ_NR, -1);
  139. if (pdata->irq_base < 0) {
  140. pr_info("%s irq_alloc_descs is failed! irq_base:%d\n", __func__, pdata->irq_base);
  141. /* getting a predefined irq_base on dt file */
  142. of_property_read_u32(np, "max77804k,irq-base", &pdata->irq_base);
  143. }
  144. pdata->wakeup = of_property_read_bool(np, "max77804k,wakeup");
  145. retval = of_get_named_gpio(np, "max77804k,wc-irq-gpio", 0);
  146. if (retval < 0)
  147. pdata->wc_irq_gpio = 0;
  148. else
  149. pdata->wc_irq_gpio = retval;
  150. pr_info("%s: irq-gpio: %u \n", __func__, pdata->irq_gpio);
  151. pr_info("%s: irq-base: %u \n", __func__, pdata->irq_base);
  152. pr_info("%s: wc-irq-gpio: %u \n", __func__, pdata->wc_irq_gpio);
  153. #ifdef CONFIG_SS_VIBRATOR
  154. of_property_read_u32(np, "haptic,max_timeout", &haptic_data->max_timeout);
  155. of_property_read_u32(np, "haptic,duty", &haptic_data->duty);
  156. of_property_read_u32(np, "haptic,period", &haptic_data->period);
  157. of_property_read_u32(np, "haptic,pwm_id", &haptic_data->pwm_id);
  158. pr_info("%s: timeout: %u \n", __func__, haptic_data->max_timeout);
  159. pr_info("%s: duty: %u \n", __func__, haptic_data->duty);
  160. pr_info("%s: period: %u \n", __func__, haptic_data->period);
  161. pr_info("%s: pwm_id: %u \n", __func__, haptic_data->pwm_id);
  162. pdata->haptic_data = haptic_data;
  163. kfree(haptic_data);
  164. #endif
  165. return 0;
  166. }
  167. static int max77804k_i2c_probe(struct i2c_client *i2c,
  168. const struct i2c_device_id *id)
  169. {
  170. struct max77804k_dev *max77804k;
  171. struct max77804k_platform_data *pdata;
  172. u8 reg_data;
  173. int ret = 0;
  174. dev_info(&i2c->dev, "%s\n", __func__);
  175. max77804k = kzalloc(sizeof(struct max77804k_dev), GFP_KERNEL);
  176. if (max77804k == NULL)
  177. return -ENOMEM;
  178. if (i2c->dev.of_node) {
  179. pdata = devm_kzalloc(&i2c->dev,
  180. sizeof(struct max77804k_platform_data),
  181. GFP_KERNEL);
  182. if (!pdata) {
  183. dev_err(&i2c->dev, "Failed to allocate memory \n");
  184. ret = -ENOMEM;
  185. goto err;
  186. }
  187. ret = of_max77804k_dt(&i2c->dev, pdata);
  188. if (ret < 0){
  189. dev_err(&i2c->dev, "Failed to get device of_node \n");
  190. ret = -ENOMEM;
  191. goto err;
  192. }
  193. /*Filling the platform data*/
  194. pdata->muic_data = &max77804k_muic;
  195. #ifdef CONFIG_REGULATOR_MAX77804K
  196. pdata->num_regulators = MAX77804K_REG_MAX;
  197. pdata->regulators = max77804k_regulators;
  198. #endif
  199. #ifdef CONFIG_LEDS_MAX77804K
  200. pdata->led_data = &max77804k_led_pdata;
  201. #endif
  202. /*pdata update to other modules*/
  203. i2c->dev.platform_data = pdata;
  204. } else
  205. pdata = i2c->dev.platform_data;
  206. i2c_set_clientdata(i2c, max77804k);
  207. max77804k->dev = &i2c->dev;
  208. max77804k->i2c = i2c;
  209. max77804k->irq = i2c->irq;
  210. if (pdata) {
  211. max77804k->irq_base = pdata->irq_base;
  212. max77804k->irq_gpio = pdata->irq_gpio;
  213. max77804k->wakeup = pdata->wakeup;
  214. gpio_tlmm_config(GPIO_CFG(max77804k->irq_gpio, 0, GPIO_CFG_INPUT,
  215. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  216. } else {
  217. ret = -EINVAL;
  218. goto err;
  219. }
  220. mutex_init(&max77804k->iolock);
  221. if (max77804k_read_reg(i2c, MAX77804K_PMIC_REG_PMIC_ID2, &reg_data) < 0) {
  222. dev_err(max77804k->dev,
  223. "device not found on this channel (this is not an error)\n");
  224. ret = -ENODEV;
  225. goto err;
  226. } else {
  227. /* print rev */
  228. max77804k->pmic_rev = (reg_data & 0x7);
  229. max77804k->pmic_ver = ((reg_data & 0xF8) >> 0x3);
  230. pr_info("%s: device found: rev.0x%x, ver.0x%x\n", __func__,
  231. max77804k->pmic_rev, max77804k->pmic_ver);
  232. }
  233. max77804k_update_reg(i2c, MAX77804K_CHG_REG_SAFEOUT_CTRL, 0x00, 0x30);
  234. max77804k->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  235. i2c_set_clientdata(max77804k->muic, max77804k);
  236. max77804k->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  237. i2c_set_clientdata(max77804k->haptic, max77804k);
  238. ret = max77804k_irq_init(max77804k);
  239. if (ret < 0)
  240. goto err_irq_init;
  241. /* disable manual reset */
  242. max77804k_read_reg(max77804k->i2c,
  243. MAX77804K_PMIC_REG_MAINCTRL1, &reg_data);
  244. reg_data &= ~(PMIC_MAINCTRL1_MREN_MASK);
  245. max77804k_write_reg(max77804k->i2c,
  246. MAX77804K_PMIC_REG_MAINCTRL1, reg_data);
  247. ret = mfd_add_devices(max77804k->dev, -1, max77804k_devs,
  248. ARRAY_SIZE(max77804k_devs), NULL, 0);
  249. if (ret < 0)
  250. goto err_mfd;
  251. device_init_wakeup(max77804k->dev, pdata->wakeup);
  252. #if defined(CONFIG_ADC_ONESHOT)
  253. /* Set oneshot mode */
  254. max77804k_update_reg(max77804k->muic, MAX77804K_MUIC_REG_CTRL4,
  255. ADC_ONESHOT<<CTRL4_ADCMODE_SHIFT, CTRL4_ADCMODE_MASK);
  256. #else
  257. /* Set continuous mode */
  258. max77804k_update_reg(max77804k->muic, MAX77804K_MUIC_REG_CTRL4,
  259. ADC_ALWAYS<<CTRL4_ADCMODE_SHIFT, CTRL4_ADCMODE_MASK);
  260. #endif
  261. return ret;
  262. err_mfd:
  263. mfd_remove_devices(max77804k->dev);
  264. max77804k_irq_exit(max77804k);
  265. err_irq_init:
  266. i2c_unregister_device(max77804k->muic);
  267. i2c_unregister_device(max77804k->haptic);
  268. err:
  269. kfree(max77804k);
  270. return ret;
  271. }
  272. static int max77804k_i2c_remove(struct i2c_client *i2c)
  273. {
  274. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  275. mfd_remove_devices(max77804k->dev);
  276. i2c_unregister_device(max77804k->muic);
  277. i2c_unregister_device(max77804k->haptic);
  278. kfree(max77804k);
  279. return 0;
  280. }
  281. static const struct i2c_device_id max77804k_i2c_id[] = {
  282. { "max77804k", TYPE_MAX77804K },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(i2c, max77804k_i2c_id);
  286. static struct of_device_id max77804k_i2c_match_table[] = {
  287. { .compatible = "max77804k,i2c", },
  288. { },
  289. };
  290. MODULE_DEVICE_TABLE(of, max77804k_i2c_match_table);
  291. #ifdef CONFIG_PM
  292. static int max77804k_suspend(struct device *dev)
  293. {
  294. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  295. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  296. if (device_may_wakeup(dev))
  297. enable_irq_wake(max77804k->irq);
  298. return 0;
  299. }
  300. static int max77804k_resume(struct device *dev)
  301. {
  302. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  303. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  304. if (device_may_wakeup(dev))
  305. disable_irq_wake(max77804k->irq);
  306. return max77804k_irq_resume(max77804k);
  307. }
  308. #else
  309. #define max77804k_suspend NULL
  310. #define max77804k_resume NULL
  311. #endif /* CONFIG_PM */
  312. #ifdef CONFIG_HIBERNATION
  313. u8 max77804k_dumpaddr_pmic[] = {
  314. MAX77804K_LED_REG_IFLASH1,
  315. MAX77804K_LED_REG_IFLASH2,
  316. MAX77804K_LED_REG_ITORCH,
  317. MAX77804K_LED_REG_ITORCHTORCHTIMER,
  318. MAX77804K_LED_REG_FLASH_TIMER,
  319. MAX77804K_LED_REG_FLASH_EN,
  320. MAX77804K_LED_REG_MAX_FLASH1,
  321. MAX77804K_LED_REG_MAX_FLASH2,
  322. MAX77804K_LED_REG_VOUT_CNTL,
  323. MAX77804K_LED_REG_VOUT_FLASH1,
  324. MAX77804K_LED_REG_FLASH_INT_STATUS,
  325. MAX77804K_PMIC_REG_TOPSYS_INT_MASK,
  326. MAX77804K_PMIC_REG_MAINCTRL1,
  327. MAX77804K_PMIC_REG_LSCNFG,
  328. MAX77804K_CHG_REG_CHG_INT_MASK,
  329. MAX77804K_CHG_REG_CHG_CNFG_00,
  330. MAX77804K_CHG_REG_CHG_CNFG_01,
  331. MAX77804K_CHG_REG_CHG_CNFG_02,
  332. MAX77804K_CHG_REG_CHG_CNFG_03,
  333. MAX77804K_CHG_REG_CHG_CNFG_04,
  334. MAX77804K_CHG_REG_CHG_CNFG_05,
  335. MAX77804K_CHG_REG_CHG_CNFG_06,
  336. MAX77804K_CHG_REG_CHG_CNFG_07,
  337. MAX77804K_CHG_REG_CHG_CNFG_08,
  338. MAX77804K_CHG_REG_CHG_CNFG_09,
  339. MAX77804K_CHG_REG_CHG_CNFG_10,
  340. MAX77804K_CHG_REG_CHG_CNFG_11,
  341. MAX77804K_CHG_REG_CHG_CNFG_12,
  342. MAX77804K_CHG_REG_CHG_CNFG_13,
  343. MAX77804K_CHG_REG_CHG_CNFG_14,
  344. MAX77804K_CHG_REG_SAFEOUT_CTRL,
  345. };
  346. u8 max77804k_dumpaddr_muic[] = {
  347. MAX77804K_MUIC_REG_INTMASK1,
  348. MAX77804K_MUIC_REG_INTMASK2,
  349. MAX77804K_MUIC_REG_INTMASK3,
  350. MAX77804K_MUIC_REG_CDETCTRL1,
  351. MAX77804K_MUIC_REG_CDETCTRL2,
  352. MAX77804K_MUIC_REG_CTRL1,
  353. MAX77804K_MUIC_REG_CTRL2,
  354. MAX77804K_MUIC_REG_CTRL3,
  355. };
  356. u8 max77804k_dumpaddr_haptic[] = {
  357. MAX77804K_HAPTIC_REG_CONFIG1,
  358. MAX77804K_HAPTIC_REG_CONFIG2,
  359. MAX77804K_HAPTIC_REG_CONFIG_CHNL,
  360. MAX77804K_HAPTIC_REG_CONFG_CYC1,
  361. MAX77804K_HAPTIC_REG_CONFG_CYC2,
  362. MAX77804K_HAPTIC_REG_CONFIG_PER1,
  363. MAX77804K_HAPTIC_REG_CONFIG_PER2,
  364. MAX77804K_HAPTIC_REG_CONFIG_PER3,
  365. MAX77804K_HAPTIC_REG_CONFIG_PER4,
  366. MAX77804K_HAPTIC_REG_CONFIG_DUTY1,
  367. MAX77804K_HAPTIC_REG_CONFIG_DUTY2,
  368. MAX77804K_HAPTIC_REG_CONFIG_PWM1,
  369. MAX77804K_HAPTIC_REG_CONFIG_PWM2,
  370. MAX77804K_HAPTIC_REG_CONFIG_PWM3,
  371. MAX77804K_HAPTIC_REG_CONFIG_PWM4,
  372. };
  373. static int max77804k_freeze(struct device *dev)
  374. {
  375. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  376. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  377. int i;
  378. for (i = 0; i < ARRAY_SIZE(max77804k_dumpaddr_pmic); i++)
  379. max77804k_read_reg(i2c, max77804k_dumpaddr_pmic[i],
  380. &max77804k->reg_pmic_dump[i]);
  381. for (i = 0; i < ARRAY_SIZE(max77804k_dumpaddr_muic); i++)
  382. max77804k_read_reg(i2c, max77804k_dumpaddr_muic[i],
  383. &max77804k->reg_muic_dump[i]);
  384. for (i = 0; i < ARRAY_SIZE(max77804k_dumpaddr_haptic); i++)
  385. max77804k_read_reg(i2c, max77804k_dumpaddr_haptic[i],
  386. &max77804k->reg_haptic_dump[i]);
  387. disable_irq(max77804k->irq);
  388. return 0;
  389. }
  390. static int max77804k_restore(struct device *dev)
  391. {
  392. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  393. struct max77804k_dev *max77804k = i2c_get_clientdata(i2c);
  394. int i;
  395. enable_irq(max77804k->irq);
  396. for (i = 0; i < ARRAY_SIZE(max77804k_dumpaddr_pmic); i++)
  397. max77804k_write_reg(i2c, max77804k_dumpaddr_pmic[i],
  398. max77804k->reg_pmic_dump[i]);
  399. for (i = 0; i < ARRAY_SIZE(max77804k_dumpaddr_muic); i++)
  400. max77804k_write_reg(i2c, max77804k_dumpaddr_muic[i],
  401. max77804k->reg_muic_dump[i]);
  402. for (i = 0; i < ARRAY_SIZE(max77804k_dumpaddr_haptic); i++)
  403. max77804k_write_reg(i2c, max77804k_dumpaddr_haptic[i],
  404. max77804k->reg_haptic_dump[i]);
  405. return 0;
  406. }
  407. #endif
  408. const struct dev_pm_ops max77804k_pm = {
  409. .suspend = max77804k_suspend,
  410. .resume = max77804k_resume,
  411. #ifdef CONFIG_HIBERNATION
  412. .freeze = max77804k_freeze,
  413. .thaw = max77804k_restore,
  414. .restore = max77804k_restore,
  415. #endif
  416. };
  417. static struct i2c_driver max77804k_i2c_driver = {
  418. .driver = {
  419. .name = "max77804k",
  420. .owner = THIS_MODULE,
  421. .pm = &max77804k_pm,
  422. .of_match_table = max77804k_i2c_match_table,
  423. },
  424. .probe = max77804k_i2c_probe,
  425. .remove = max77804k_i2c_remove,
  426. .id_table = max77804k_i2c_id,
  427. };
  428. static int __init max77804k_i2c_init(void)
  429. {
  430. return i2c_add_driver(&max77804k_i2c_driver);
  431. }
  432. /* init early so consumer devices can complete system boot */
  433. subsys_initcall(max77804k_i2c_init);
  434. static void __exit max77804k_i2c_exit(void)
  435. {
  436. i2c_del_driver(&max77804k_i2c_driver);
  437. }
  438. module_exit(max77804k_i2c_exit);
  439. MODULE_DESCRIPTION("MAXIM 77804k multi-function core driver");
  440. MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
  441. MODULE_LICENSE("GPL");