max77803.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * max77803.c - mfd core driver for the Maxim 77803
  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/max77803.h>
  30. #include <linux/mfd/max77803-private.h>
  31. #include <linux/regulator/machine.h>
  32. //#include <mach/sec_debug.h>
  33. #include <linux/mfd/pm8xxx/misc.h>
  34. #if defined (CONFIG_OF)
  35. #include <linux/of_device.h>
  36. #include <linux/of_gpio.h>
  37. #endif
  38. #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
  39. #define I2C_ADDR_MUIC (0x4A >> 1)
  40. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  41. static struct mfd_cell max77803_devs[] = {
  42. { .name = "max77803-charger", },
  43. { .name = "max77803-led", },
  44. { .name = "max77803-muic", },
  45. { .name = "max77803-safeout", },
  46. { .name = "max77803-haptic", },
  47. };
  48. int max77803_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  49. {
  50. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  51. int ret;
  52. mutex_lock(&max77803->iolock);
  53. ret = i2c_smbus_read_byte_data(i2c, reg);
  54. mutex_unlock(&max77803->iolock);
  55. if (ret < 0) {
  56. dev_err(max77803->dev,
  57. "%s, reg(0x%x), ret(%d)\n", __func__, reg, ret);
  58. return ret;
  59. }
  60. ret &= 0xff;
  61. *dest = ret;
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(max77803_read_reg);
  65. int max77803_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  66. {
  67. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  68. int ret;
  69. mutex_lock(&max77803->iolock);
  70. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  71. mutex_unlock(&max77803->iolock);
  72. if (ret < 0)
  73. return ret;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL_GPL(max77803_bulk_read);
  77. int max77803_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  78. {
  79. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  80. int ret;
  81. mutex_lock(&max77803->iolock);
  82. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  83. if (ret < 0)
  84. dev_err(max77803->dev,
  85. "%s, reg(0x%x), ret(%d)\n", __func__, reg, ret);
  86. mutex_unlock(&max77803->iolock);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL_GPL(max77803_write_reg);
  90. int max77803_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  91. {
  92. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  93. int ret;
  94. mutex_lock(&max77803->iolock);
  95. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  96. mutex_unlock(&max77803->iolock);
  97. if (ret < 0)
  98. return ret;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(max77803_bulk_write);
  102. int max77803_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  103. {
  104. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  105. int ret;
  106. mutex_lock(&max77803->iolock);
  107. ret = i2c_smbus_read_byte_data(i2c, reg);
  108. if (ret >= 0) {
  109. u8 old_val = ret & 0xff;
  110. u8 new_val = (val & mask) | (old_val & (~mask));
  111. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  112. }
  113. mutex_unlock(&max77803->iolock);
  114. return ret;
  115. }
  116. EXPORT_SYMBOL_GPL(max77803_update_reg);
  117. static int of_max77803_dt(struct device *dev, struct max77803_platform_data *pdata)
  118. {
  119. struct device_node *np = dev->of_node;
  120. #ifdef CONFIG_SS_VIBRATOR
  121. struct max77803_haptic_platform_data *haptic_data;
  122. #endif
  123. int retval = 0;
  124. #ifdef CONFIG_SS_VIBRATOR
  125. haptic_data = kzalloc(sizeof(struct max77803_haptic_platform_data), GFP_KERNEL);
  126. if (haptic_data == NULL)
  127. return -ENOMEM;
  128. #endif
  129. if(!np)
  130. return -EINVAL;
  131. pdata->irq_gpio = of_get_named_gpio_flags(np, "max77803,irq-gpio",
  132. 0, &pdata->irq_gpio_flags);
  133. of_property_read_u32(np, "max77803,irq-base", &pdata->irq_base);
  134. pdata->wakeup = of_property_read_bool(np, "max77803,wakeup");
  135. retval = of_get_named_gpio(np, "max77803,wc-irq-gpio", 0);
  136. if (retval < 0)
  137. pdata->wc_irq_gpio = 0;
  138. else
  139. pdata->wc_irq_gpio = retval;
  140. pr_info("%s: irq-gpio: %u \n", __func__, pdata->irq_gpio);
  141. pr_info("%s: irq-base: %u \n", __func__, pdata->irq_base);
  142. pr_info("%s: wc-irq-gpio: %u \n", __func__, pdata->wc_irq_gpio);
  143. #ifdef CONFIG_SS_VIBRATOR
  144. of_property_read_u32(np, "haptic,max_timeout", &haptic_data->max_timeout);
  145. of_property_read_u32(np, "haptic,duty", &haptic_data->duty);
  146. of_property_read_u32(np, "haptic,period", &haptic_data->period);
  147. of_property_read_u32(np, "haptic,pwm_id", &haptic_data->pwm_id);
  148. pr_info("%s: timeout: %u \n", __func__, haptic_data->max_timeout);
  149. pr_info("%s: duty: %u \n", __func__, haptic_data->duty);
  150. pr_info("%s: period: %u \n", __func__, haptic_data->period);
  151. pr_info("%s: pwm_id: %u \n", __func__, haptic_data->pwm_id);
  152. pdata->haptic_data = haptic_data;
  153. #endif
  154. return 0;
  155. }
  156. static int max77803_i2c_probe(struct i2c_client *i2c,
  157. const struct i2c_device_id *id)
  158. {
  159. struct max77803_dev *max77803;
  160. struct max77803_platform_data *pdata;
  161. u8 reg_data;
  162. int ret = 0;
  163. dev_info(&i2c->dev, "%s\n", __func__);
  164. max77803 = kzalloc(sizeof(struct max77803_dev), GFP_KERNEL);
  165. if (max77803 == NULL)
  166. return -ENOMEM;
  167. if (i2c->dev.of_node) {
  168. pdata = devm_kzalloc(&i2c->dev,
  169. sizeof(struct max77803_platform_data),
  170. GFP_KERNEL);
  171. if (!pdata) {
  172. dev_err(&i2c->dev, "Failed to allocate memory \n");
  173. ret = -ENOMEM;
  174. goto err;
  175. }
  176. ret = of_max77803_dt(&i2c->dev, pdata);
  177. if (ret < 0){
  178. dev_err(&i2c->dev, "Failed to get device of_node \n");
  179. return ret;
  180. }
  181. /*Filling the platform data*/
  182. pdata->muic_data = &max77803_muic;
  183. #ifdef CONFIG_REGULATOR_MAX77803
  184. pdata->num_regulators = MAX77803_REG_MAX;
  185. pdata->regulators = max77803_regulators;
  186. #endif
  187. #ifdef CONFIG_LEDS_MAX77803
  188. pdata->led_data = &max77803_led_pdata;
  189. #endif
  190. /*pdata update to other modules*/
  191. i2c->dev.platform_data = pdata;
  192. } else
  193. pdata = i2c->dev.platform_data;
  194. i2c_set_clientdata(i2c, max77803);
  195. max77803->dev = &i2c->dev;
  196. max77803->i2c = i2c;
  197. max77803->irq = i2c->irq;
  198. // max77803->type = id->driver_data;
  199. if (pdata) {
  200. max77803->irq_base = pdata->irq_base;
  201. max77803->irq_gpio = pdata->irq_gpio;
  202. max77803->wakeup = pdata->wakeup;
  203. gpio_tlmm_config(GPIO_CFG(max77803->irq_gpio, 0, GPIO_CFG_INPUT,
  204. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  205. } else {
  206. ret = -EINVAL;
  207. goto err;
  208. }
  209. mutex_init(&max77803->iolock);
  210. if (max77803_read_reg(i2c, MAX77803_PMIC_REG_PMIC_ID2, &reg_data) < 0) {
  211. dev_err(max77803->dev,
  212. "device not found on this channel (this is not an error)\n");
  213. ret = -ENODEV;
  214. goto err;
  215. } else {
  216. /* print rev */
  217. max77803->pmic_rev = (reg_data & 0x7);
  218. max77803->pmic_ver = ((reg_data & 0xF8) >> 0x3);
  219. pr_info("%s: device found: rev.0x%x, ver.0x%x\n", __func__,
  220. max77803->pmic_rev, max77803->pmic_ver);
  221. }
  222. #if 0
  223. #if defined(CONFIG_MACH_JF_VZW) || defined(CONFIG_MACH_JF_LGT)
  224. if (kernel_sec_get_debug_level() == KERNEL_SEC_DEBUG_LEVEL_LOW) {
  225. pm8xxx_hard_reset_config(PM8XXX_DISABLE_HARD_RESET);
  226. max77803_write_reg(i2c, MAX77803_PMIC_REG_MAINCTRL1, 0x04);
  227. } else {
  228. pm8xxx_hard_reset_config(PM8XXX_DISABLE_HARD_RESET);
  229. max77803_write_reg(i2c, MAX77803_PMIC_REG_MAINCTRL1, 0x0c);
  230. }
  231. #else
  232. if (kernel_sec_get_debug_level() == KERNEL_SEC_DEBUG_LEVEL_LOW) {
  233. max77803_write_reg(i2c, MAX77803_PMIC_REG_MAINCTRL1, 0x04);
  234. } else {
  235. pm8xxx_hard_reset_config(PM8XXX_DISABLE_HARD_RESET);
  236. max77803_write_reg(i2c, MAX77803_PMIC_REG_MAINCTRL1, 0x0c);
  237. }
  238. #endif
  239. #endif
  240. max77803_update_reg(i2c, MAX77803_CHG_REG_SAFEOUT_CTRL, 0x00, 0x30);
  241. max77803->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  242. i2c_set_clientdata(max77803->muic, max77803);
  243. max77803->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  244. i2c_set_clientdata(max77803->haptic, max77803);
  245. ret = max77803_irq_init(max77803);
  246. if (ret < 0)
  247. goto err_irq_init;
  248. ret = mfd_add_devices(max77803->dev, -1, max77803_devs,
  249. ARRAY_SIZE(max77803_devs), NULL, 0);
  250. if (ret < 0)
  251. goto err_mfd;
  252. device_init_wakeup(max77803->dev, pdata->wakeup);
  253. return ret;
  254. err_mfd:
  255. mfd_remove_devices(max77803->dev);
  256. max77803_irq_exit(max77803);
  257. err_irq_init:
  258. i2c_unregister_device(max77803->muic);
  259. i2c_unregister_device(max77803->haptic);
  260. err:
  261. kfree(max77803);
  262. return ret;
  263. }
  264. static int max77803_i2c_remove(struct i2c_client *i2c)
  265. {
  266. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  267. mfd_remove_devices(max77803->dev);
  268. i2c_unregister_device(max77803->muic);
  269. i2c_unregister_device(max77803->haptic);
  270. kfree(max77803);
  271. return 0;
  272. }
  273. static const struct i2c_device_id max77803_i2c_id[] = {
  274. { "max77803", TYPE_MAX77803 },
  275. { }
  276. };
  277. MODULE_DEVICE_TABLE(i2c, max77803_i2c_id);
  278. static struct of_device_id max77803_i2c_match_table[] = {
  279. { .compatible = "max77803,i2c", },
  280. { },
  281. };
  282. MODULE_DEVICE_TABLE(of, max77803_i2c_match_table);
  283. #ifdef CONFIG_PM
  284. static int max77803_suspend(struct device *dev)
  285. {
  286. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  287. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  288. if (device_may_wakeup(dev))
  289. enable_irq_wake(max77803->irq);
  290. return 0;
  291. }
  292. static int max77803_resume(struct device *dev)
  293. {
  294. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  295. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  296. if (device_may_wakeup(dev))
  297. disable_irq_wake(max77803->irq);
  298. return max77803_irq_resume(max77803);
  299. }
  300. #else
  301. #define max77803_suspend NULL
  302. #define max77803_resume NULL
  303. #endif /* CONFIG_PM */
  304. #ifdef CONFIG_HIBERNATION
  305. u8 max77803_dumpaddr_pmic[] = {
  306. MAX77803_LED_REG_IFLASH1,
  307. MAX77803_LED_REG_IFLASH2,
  308. MAX77803_LED_REG_ITORCH,
  309. MAX77803_LED_REG_ITORCHTORCHTIMER,
  310. MAX77803_LED_REG_FLASH_TIMER,
  311. MAX77803_LED_REG_FLASH_EN,
  312. MAX77803_LED_REG_MAX_FLASH1,
  313. MAX77803_LED_REG_MAX_FLASH2,
  314. MAX77803_LED_REG_VOUT_CNTL,
  315. MAX77803_LED_REG_VOUT_FLASH1,
  316. MAX77803_LED_REG_FLASH_INT_STATUS,
  317. MAX77803_PMIC_REG_TOPSYS_INT_MASK,
  318. MAX77803_PMIC_REG_MAINCTRL1,
  319. MAX77803_PMIC_REG_LSCNFG,
  320. MAX77803_CHG_REG_CHG_INT_MASK,
  321. MAX77803_CHG_REG_CHG_CNFG_00,
  322. MAX77803_CHG_REG_CHG_CNFG_01,
  323. MAX77803_CHG_REG_CHG_CNFG_02,
  324. MAX77803_CHG_REG_CHG_CNFG_03,
  325. MAX77803_CHG_REG_CHG_CNFG_04,
  326. MAX77803_CHG_REG_CHG_CNFG_05,
  327. MAX77803_CHG_REG_CHG_CNFG_06,
  328. MAX77803_CHG_REG_CHG_CNFG_07,
  329. MAX77803_CHG_REG_CHG_CNFG_08,
  330. MAX77803_CHG_REG_CHG_CNFG_09,
  331. MAX77803_CHG_REG_CHG_CNFG_10,
  332. MAX77803_CHG_REG_CHG_CNFG_11,
  333. MAX77803_CHG_REG_CHG_CNFG_12,
  334. MAX77803_CHG_REG_CHG_CNFG_13,
  335. MAX77803_CHG_REG_CHG_CNFG_14,
  336. MAX77803_CHG_REG_SAFEOUT_CTRL,
  337. };
  338. u8 max77803_dumpaddr_muic[] = {
  339. MAX77803_MUIC_REG_INTMASK1,
  340. MAX77803_MUIC_REG_INTMASK2,
  341. MAX77803_MUIC_REG_INTMASK3,
  342. MAX77803_MUIC_REG_CDETCTRL1,
  343. MAX77803_MUIC_REG_CDETCTRL2,
  344. MAX77803_MUIC_REG_CTRL1,
  345. MAX77803_MUIC_REG_CTRL2,
  346. MAX77803_MUIC_REG_CTRL3,
  347. };
  348. u8 max77803_dumpaddr_haptic[] = {
  349. MAX77803_HAPTIC_REG_CONFIG1,
  350. MAX77803_HAPTIC_REG_CONFIG2,
  351. MAX77803_HAPTIC_REG_CONFIG_CHNL,
  352. MAX77803_HAPTIC_REG_CONFG_CYC1,
  353. MAX77803_HAPTIC_REG_CONFG_CYC2,
  354. MAX77803_HAPTIC_REG_CONFIG_PER1,
  355. MAX77803_HAPTIC_REG_CONFIG_PER2,
  356. MAX77803_HAPTIC_REG_CONFIG_PER3,
  357. MAX77803_HAPTIC_REG_CONFIG_PER4,
  358. MAX77803_HAPTIC_REG_CONFIG_DUTY1,
  359. MAX77803_HAPTIC_REG_CONFIG_DUTY2,
  360. MAX77803_HAPTIC_REG_CONFIG_PWM1,
  361. MAX77803_HAPTIC_REG_CONFIG_PWM2,
  362. MAX77803_HAPTIC_REG_CONFIG_PWM3,
  363. MAX77803_HAPTIC_REG_CONFIG_PWM4,
  364. };
  365. static int max77803_freeze(struct device *dev)
  366. {
  367. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  368. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  369. int i;
  370. for (i = 0; i < ARRAY_SIZE(max77803_dumpaddr_pmic); i++)
  371. max77803_read_reg(i2c, max77803_dumpaddr_pmic[i],
  372. &max77803->reg_pmic_dump[i]);
  373. for (i = 0; i < ARRAY_SIZE(max77803_dumpaddr_muic); i++)
  374. max77803_read_reg(i2c, max77803_dumpaddr_muic[i],
  375. &max77803->reg_muic_dump[i]);
  376. for (i = 0; i < ARRAY_SIZE(max77803_dumpaddr_haptic); i++)
  377. max77803_read_reg(i2c, max77803_dumpaddr_haptic[i],
  378. &max77803->reg_haptic_dump[i]);
  379. disable_irq(max77803->irq);
  380. return 0;
  381. }
  382. static int max77803_restore(struct device *dev)
  383. {
  384. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  385. struct max77803_dev *max77803 = i2c_get_clientdata(i2c);
  386. int i;
  387. enable_irq(max77803->irq);
  388. for (i = 0; i < ARRAY_SIZE(max77803_dumpaddr_pmic); i++)
  389. max77803_write_reg(i2c, max77803_dumpaddr_pmic[i],
  390. max77803->reg_pmic_dump[i]);
  391. for (i = 0; i < ARRAY_SIZE(max77803_dumpaddr_muic); i++)
  392. max77803_write_reg(i2c, max77803_dumpaddr_muic[i],
  393. max77803->reg_muic_dump[i]);
  394. for (i = 0; i < ARRAY_SIZE(max77803_dumpaddr_haptic); i++)
  395. max77803_write_reg(i2c, max77803_dumpaddr_haptic[i],
  396. max77803->reg_haptic_dump[i]);
  397. return 0;
  398. }
  399. #endif
  400. const struct dev_pm_ops max77803_pm = {
  401. .suspend = max77803_suspend,
  402. .resume = max77803_resume,
  403. #ifdef CONFIG_HIBERNATION
  404. .freeze = max77803_freeze,
  405. .thaw = max77803_restore,
  406. .restore = max77803_restore,
  407. #endif
  408. };
  409. static struct i2c_driver max77803_i2c_driver = {
  410. .driver = {
  411. .name = "max77803",
  412. .owner = THIS_MODULE,
  413. .pm = &max77803_pm,
  414. .of_match_table = max77803_i2c_match_table,
  415. },
  416. .probe = max77803_i2c_probe,
  417. .remove = max77803_i2c_remove,
  418. .id_table = max77803_i2c_id,
  419. };
  420. //module_i2c_driver(max77803_i2c_driver);
  421. static int __init max77803_i2c_init(void)
  422. {
  423. return i2c_add_driver(&max77803_i2c_driver);
  424. }
  425. /* init early so consumer devices can complete system boot */
  426. subsys_initcall(max77803_i2c_init);
  427. static void __exit max77803_i2c_exit(void)
  428. {
  429. i2c_del_driver(&max77803_i2c_driver);
  430. }
  431. module_exit(max77803_i2c_exit);
  432. MODULE_DESCRIPTION("MAXIM 77803 multi-function core driver");
  433. MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
  434. MODULE_LICENSE("GPL");