max77828.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * max77828.c - mfd core driver for the Maxim 77828
  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/max77828.h>
  30. #include <linux/mfd/max77828-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 (0x92 >> 1) /* Top sys, Haptic */
  38. #define I2C_ADDR_MUIC (0x4A >> 1)
  39. #define I2C_ADDR_LED (0x94 >> 1)
  40. static struct mfd_cell max77828_devs[] = {
  41. { .name = "max77828-muic", },
  42. { .name = "max77828-haptic", },
  43. { .name = "max77828-led",},
  44. };
  45. int max77828_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  46. {
  47. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  48. int ret;
  49. mutex_lock(&max77828->iolock);
  50. ret = i2c_smbus_read_byte_data(i2c, reg);
  51. mutex_unlock(&max77828->iolock);
  52. if (ret < 0) {
  53. dev_err(max77828->dev, "%s, reg(0x%x), ret(%d)\n", __func__, reg, ret);
  54. return ret;
  55. }
  56. ret &= 0xff;
  57. *dest = ret;
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(max77828_read_reg);
  61. int max77828_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  62. {
  63. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  64. int ret;
  65. mutex_lock(&max77828->iolock);
  66. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  67. mutex_unlock(&max77828->iolock);
  68. if (ret < 0)
  69. return ret;
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(max77828_bulk_read);
  73. int max77828_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  74. {
  75. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  76. int ret;
  77. mutex_lock(&max77828->iolock);
  78. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  79. mutex_unlock(&max77828->iolock);
  80. if (ret < 0)
  81. dev_err(max77828->dev,
  82. "%s, reg(0x%x), ret(%d)\n", __func__, reg, ret);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(max77828_write_reg);
  86. int max77828_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  87. {
  88. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  89. int ret;
  90. mutex_lock(&max77828->iolock);
  91. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  92. mutex_unlock(&max77828->iolock);
  93. if (ret < 0)
  94. return ret;
  95. return 0;
  96. }
  97. EXPORT_SYMBOL_GPL(max77828_bulk_write);
  98. int max77828_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  99. {
  100. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  101. int ret;
  102. mutex_lock(&max77828->iolock);
  103. ret = i2c_smbus_read_byte_data(i2c, reg);
  104. if (ret >= 0) {
  105. u8 old_val = ret & 0xff;
  106. u8 new_val = (val & mask) | (old_val & (~mask));
  107. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  108. }
  109. mutex_unlock(&max77828->iolock);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL_GPL(max77828_update_reg);
  113. static int of_max77828_dt(struct device *dev, struct max77828_platform_data *pdata)
  114. {
  115. struct device_node *np = dev->of_node;
  116. int retval = 0;
  117. struct max77828_haptic_platform_data *haptic_data;
  118. haptic_data = kzalloc(sizeof(struct max77828_haptic_platform_data), GFP_KERNEL);
  119. if (haptic_data == NULL)
  120. return -ENOMEM;
  121. if(!np)
  122. return -EINVAL;
  123. pdata->irq_gpio = of_get_named_gpio_flags(np, "max77828,irq-gpio",
  124. 0, &pdata->irq_gpio_flags);
  125. of_property_read_u32(np, "max77828,irq-base", &pdata->irq_base);
  126. pdata->wakeup = of_property_read_bool(np, "max77828,wakeup");
  127. retval = of_get_named_gpio(np, "max77828,wc-irq-gpio", 0);
  128. if (retval < 0)
  129. pdata->wc_irq_gpio = 0;
  130. else
  131. pdata->wc_irq_gpio = retval;
  132. pr_info("%s: irq-gpio: %u \n", __func__, pdata->irq_gpio);
  133. pr_info("%s: irq-base: %u \n", __func__, pdata->irq_base);
  134. pr_info("%s: wc-irq-gpio: %u \n", __func__, pdata->wc_irq_gpio);
  135. #ifdef CONFIG_SS_VIBRATOR
  136. if (!of_property_read_u32(np, "haptic,mode", &haptic_data->mode))
  137. haptic_data->mode = 0;
  138. if (!of_property_read_u32(np, "haptic,divisor", &haptic_data->divisor))
  139. haptic_data->divisor = 128;
  140. pr_info("%s: mode: %u \n", __func__, haptic_data->mode);
  141. pr_info("%s: divisor: %u \n", __func__, haptic_data->divisor);
  142. pdata->haptic_data = haptic_data;
  143. #endif
  144. return 0;
  145. }
  146. static int max77828_i2c_probe(struct i2c_client *i2c,
  147. const struct i2c_device_id *id)
  148. {
  149. struct max77828_dev *max77828;
  150. struct max77828_platform_data *pdata = i2c->dev.platform_data;
  151. u8 reg_data;
  152. int ret = 0;
  153. dev_info(&i2c->dev, "%s\n", __func__);
  154. max77828 = kzalloc(sizeof(struct max77828_dev), GFP_KERNEL);
  155. if (!max77828) {
  156. dev_err(&i2c->dev, "%s: Failed to alloc mem for max77828\n", __func__);
  157. return -ENOMEM;
  158. }
  159. if (i2c->dev.of_node) {
  160. pdata = devm_kzalloc(&i2c->dev, sizeof(struct max77828_platform_data),
  161. GFP_KERNEL);
  162. if (!pdata) {
  163. dev_err(&i2c->dev, "Failed to allocate memory \n");
  164. ret = -ENOMEM;
  165. goto err;
  166. }
  167. ret = of_max77828_dt(&i2c->dev, pdata);
  168. if (ret < 0){
  169. dev_err(&i2c->dev, "Failed to get device of_node \n");
  170. return ret;
  171. }
  172. /*pdata update to other modules*/
  173. pdata->muic_data = &max77828_muic;
  174. #ifdef CONFIG_LEDS_MAX77828
  175. pdata->led_data = &max77828_led_pdata;
  176. #endif
  177. i2c->dev.platform_data = pdata;
  178. } else
  179. pdata = i2c->dev.platform_data;
  180. max77828->dev = &i2c->dev;
  181. max77828->i2c = i2c;
  182. max77828->irq = i2c->irq;
  183. if (pdata) {
  184. max77828->pdata = pdata;
  185. max77828->irq_base = pdata->irq_base;
  186. max77828->irq_gpio = pdata->irq_gpio;
  187. max77828->wakeup = pdata->wakeup;
  188. gpio_tlmm_config(GPIO_CFG(max77828->irq_gpio, 0, GPIO_CFG_INPUT,
  189. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  190. } else {
  191. ret = -EINVAL;
  192. goto err;
  193. }
  194. mutex_init(&max77828->iolock);
  195. i2c_set_clientdata(i2c, max77828);
  196. if (max77828_read_reg(i2c, MAX77828_PMIC_REG_PMICREV, &reg_data) < 0) {
  197. dev_err(max77828->dev,
  198. "device not found on this channel (this is not an error)\n");
  199. ret = -ENODEV;
  200. goto err;
  201. } else {
  202. /* print rev */
  203. max77828->pmic_rev = (reg_data & 0x7);
  204. max77828->pmic_ver = ((reg_data & 0xF8) >> 0x3);
  205. pr_info("%s: device found: rev.0x%x, ver.0x%x\n", __func__,
  206. max77828->pmic_rev, max77828->pmic_ver);
  207. }
  208. max77828->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  209. i2c_set_clientdata(max77828->muic, max77828);
  210. max77828->led = i2c_new_dummy(i2c->adapter, I2C_ADDR_LED);
  211. i2c_set_clientdata(max77828->led, max77828);
  212. ret = max77828_irq_init(max77828);
  213. if (ret < 0)
  214. goto err_irq_init;
  215. ret = mfd_add_devices(max77828->dev, -1, max77828_devs,
  216. ARRAY_SIZE(max77828_devs), NULL, 0);
  217. if (ret < 0)
  218. goto err_mfd;
  219. device_init_wakeup(max77828->dev, pdata->wakeup);
  220. return ret;
  221. err_mfd:
  222. mfd_remove_devices(max77828->dev);
  223. max77828_irq_exit(max77828);
  224. err_irq_init:
  225. i2c_unregister_device(max77828->muic);
  226. i2c_unregister_device(max77828->led);
  227. err:
  228. kfree(max77828);
  229. return ret;
  230. }
  231. static int max77828_i2c_remove(struct i2c_client *i2c)
  232. {
  233. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  234. mfd_remove_devices(max77828->dev);
  235. i2c_unregister_device(max77828->muic);
  236. i2c_unregister_device(max77828->led);
  237. kfree(max77828);
  238. return 0;
  239. }
  240. static const struct i2c_device_id max77828_i2c_id[] = {
  241. { "max77828", TYPE_MAX77828 },
  242. { }
  243. };
  244. MODULE_DEVICE_TABLE(i2c, max77828_i2c_id);
  245. static struct of_device_id max77828_i2c_match_table[] = {
  246. { .compatible = "max77828,i2c", },
  247. { },
  248. };
  249. MODULE_DEVICE_TABLE(of, max77828_i2c_match_table);
  250. #ifdef CONFIG_PM
  251. static int max77828_suspend(struct device *dev)
  252. {
  253. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  254. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  255. if (device_may_wakeup(dev))
  256. enable_irq_wake(max77828->irq);
  257. return 0;
  258. }
  259. static int max77828_resume(struct device *dev)
  260. {
  261. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  262. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  263. if (device_may_wakeup(dev))
  264. disable_irq_wake(max77828->irq);
  265. return max77828_irq_resume(max77828);
  266. }
  267. #else
  268. #define max77828_suspend NULL
  269. #define max77828_resume NULL
  270. #endif /* CONFIG_PM */
  271. #ifdef CONFIG_HIBERNATION
  272. #if 0
  273. u8 max77828_dumpaddr_pmic[] = {
  274. MAX77828_LED_REG_IFLASH1,
  275. MAX77828_LED_REG_IFLASH2,
  276. MAX77828_LED_REG_ITORCH,
  277. MAX77828_LED_REG_ITORCHTORCHTIMER,
  278. MAX77828_LED_REG_FLASH_TIMER,
  279. MAX77828_LED_REG_FLASH_EN,
  280. MAX77828_LED_REG_MAX_FLASH1,
  281. MAX77828_LED_REG_MAX_FLASH2,
  282. MAX77828_LED_REG_VOUT_CNTL,
  283. MAX77828_LED_REG_VOUT_FLASH1,
  284. MAX77828_LED_REG_FLASH_INT_STATUS,
  285. MAX77828_PMIC_REG_TOPSYS_INT_MASK,
  286. MAX77828_PMIC_REG_MAINCTRL1,
  287. MAX77828_PMIC_REG_LSCNFG,
  288. };
  289. #endif
  290. u8 max77828_dumpaddr_muic[] = {
  291. MAX77828_MUIC_REG_INTMASK1,
  292. MAX77828_MUIC_REG_INTMASK2,
  293. MAX77828_MUIC_REG_INTMASK3,
  294. MAX77828_MUIC_REG_CDETCTRL1,
  295. MAX77828_MUIC_REG_CDETCTRL2,
  296. MAX77828_MUIC_REG_CTRL1,
  297. MAX77828_MUIC_REG_CTRL2,
  298. MAX77828_MUIC_REG_CTRL3,
  299. MAX77828_MUIC_REG_CTRL4,
  300. };
  301. #if 0
  302. u8 max77828_dumpaddr_haptic[] = {
  303. MAX77828_HAPTIC_REG_CONFIG1,
  304. MAX77828_HAPTIC_REG_CONFIG2,
  305. MAX77828_HAPTIC_REG_CONFIG_CHNL,
  306. MAX77828_HAPTIC_REG_CONFG_CYC1,
  307. MAX77828_HAPTIC_REG_CONFG_CYC2,
  308. MAX77828_HAPTIC_REG_CONFIG_PER1,
  309. MAX77828_HAPTIC_REG_CONFIG_PER2,
  310. MAX77828_HAPTIC_REG_CONFIG_PER3,
  311. MAX77828_HAPTIC_REG_CONFIG_PER4,
  312. MAX77828_HAPTIC_REG_CONFIG_DUTY1,
  313. MAX77828_HAPTIC_REG_CONFIG_DUTY2,
  314. MAX77828_HAPTIC_REG_CONFIG_PWM1,
  315. MAX77828_HAPTIC_REG_CONFIG_PWM2,
  316. MAX77828_HAPTIC_REG_CONFIG_PWM3,
  317. MAX77828_HAPTIC_REG_CONFIG_PWM4,
  318. };
  319. #endif
  320. u8 max77828_dumpaddr_led[] = {
  321. MAX77828_RGBLED_REG_LEDEN,
  322. MAX77828_RGBLED_REG_LED0BRT,
  323. MAX77828_RGBLED_REG_LED1BRT,
  324. MAX77828_RGBLED_REG_LED2BRT,
  325. MAX77828_RGBLED_REG_LED3BRT,
  326. MAX77828_RGBLED_REG_LEDBLNK,
  327. MAX77828_RGBLED_REG_LEDRMP,
  328. };
  329. static int max77828_freeze(struct device *dev)
  330. {
  331. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  332. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  333. int i;
  334. for (i = 0; i < ARRAY_SIZE(max77828_dumpaddr_pmic); i++)
  335. max77828_read_reg(i2c, max77828_dumpaddr_pmic[i],
  336. &max77828->reg_pmic_dump[i]);
  337. for (i = 0; i < ARRAY_SIZE(max77828_dumpaddr_muic); i++)
  338. max77828_read_reg(i2c, max77828_dumpaddr_muic[i],
  339. &max77828->reg_muic_dump[i]);
  340. for (i = 0; i < ARRAY_SIZE(max77828_dumpaddr_led); i++)
  341. max77828_read_reg(i2c, max77828_dumpaddr_led[i],
  342. &max77828->reg_led_dump[i]);
  343. disable_irq(max77828->irq);
  344. return 0;
  345. }
  346. static int max77828_restore(struct device *dev)
  347. {
  348. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  349. struct max77828_dev *max77828 = i2c_get_clientdata(i2c);
  350. int i;
  351. enable_irq(max77828->irq);
  352. for (i = 0; i < ARRAY_SIZE(max77828_dumpaddr_pmic); i++)
  353. max77828_write_reg(i2c, max77828_dumpaddr_pmic[i],
  354. max77828->reg_pmic_dump[i]);
  355. for (i = 0; i < ARRAY_SIZE(max77828_dumpaddr_muic); i++)
  356. max77828_write_reg(i2c, max77828_dumpaddr_muic[i],
  357. max77828->reg_muic_dump[i]);
  358. for (i = 0; i < ARRAY_SIZE(max77828_dumpaddr_led); i++)
  359. max77828_write_reg(i2c, max77828_dumpaddr_led[i],
  360. max77828->reg_led_dump[i]);
  361. return 0;
  362. }
  363. #endif
  364. const struct dev_pm_ops max77828_pm = {
  365. .suspend = max77828_suspend,
  366. .resume = max77828_resume,
  367. #ifdef CONFIG_HIBERNATION
  368. .freeze = max77828_freeze,
  369. .thaw = max77828_restore,
  370. .restore = max77828_restore,
  371. #endif
  372. };
  373. static struct i2c_driver max77828_i2c_driver = {
  374. .driver = {
  375. .name = "max77828",
  376. .owner = THIS_MODULE,
  377. .pm = &max77828_pm,
  378. .of_match_table = max77828_i2c_match_table,
  379. },
  380. .probe = max77828_i2c_probe,
  381. .remove = max77828_i2c_remove,
  382. .id_table = max77828_i2c_id,
  383. };
  384. static int __init max77828_i2c_init(void)
  385. {
  386. return i2c_add_driver(&max77828_i2c_driver);
  387. }
  388. /* init early so consumer devices can complete system boot */
  389. subsys_initcall(max77828_i2c_init);
  390. static void __exit max77828_i2c_exit(void)
  391. {
  392. i2c_del_driver(&max77828_i2c_driver);
  393. }
  394. module_exit(max77828_i2c_exit);
  395. MODULE_DESCRIPTION("MAXIM 77828 multi-function core driver");
  396. MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
  397. MODULE_LICENSE("GPL");