max77828_haptic.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * haptic motor driver for max77828_haptic.c
  3. *
  4. * Copyright (C) 2013 Ravi Shekhar Singh <shekhar.sr@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/pwm.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/clk.h>
  18. #include <linux/i2c.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/mfd/max77828.h>
  21. #include <linux/mfd/max77828-private.h>
  22. struct max77828_haptic_data {
  23. struct max77828_dev *max77828;
  24. struct i2c_client *i2c;
  25. struct max77828_haptic_platform_data *pdata;
  26. u8 reg;
  27. spinlock_t lock;
  28. bool running;
  29. };
  30. struct max77828_haptic_data *g_hap_data;
  31. static void max77828_haptic_i2c(struct max77828_haptic_data *hap_data, bool en)
  32. {
  33. int ret;
  34. u8 value = hap_data->reg;
  35. pr_debug("[VIB] %s %d\n", __func__, en);
  36. if (en)
  37. value = 0xC3;
  38. ret = max77828_write_reg(hap_data->i2c, MAX77828_PMIC_REG_MCONFIG, value);
  39. if (ret)
  40. pr_err("[VIB] i2c write error %d\n", ret);
  41. ret = max77828_read_reg(hap_data->i2c, MAX77828_PMIC_REG_MCONFIG, &value);
  42. if (ret)
  43. pr_err("[VIB] i2c read error %d\n", ret);
  44. }
  45. #ifdef CONFIG_SS_VIBRATOR
  46. void max77828_vibtonz_en(bool en)
  47. {
  48. if (g_hap_data == NULL) {
  49. return ;
  50. }
  51. if (en) {
  52. if (g_hap_data->running)
  53. return;
  54. max77828_haptic_i2c(g_hap_data, true);
  55. g_hap_data->running = true;
  56. } else {
  57. if (!g_hap_data->running)
  58. return;
  59. max77828_haptic_i2c(g_hap_data, false);
  60. g_hap_data->running = false;
  61. }
  62. }
  63. EXPORT_SYMBOL(max77828_vibtonz_en);
  64. #endif
  65. static int __devinit max77828_haptic_probe(struct platform_device *pdev)
  66. {
  67. int error = 0;
  68. struct max77828_dev *max77828 = dev_get_drvdata(pdev->dev.parent);
  69. struct max77828_platform_data *max77828_pdata
  70. = dev_get_platdata(max77828->dev);
  71. #ifdef CONFIG_SS_VIBRATOR
  72. struct max77828_haptic_platform_data *pdata
  73. = max77828_pdata->haptic_data;
  74. #endif
  75. struct max77828_haptic_data *hap_data;
  76. pr_err("[VIB] ++ %s\n", __func__);
  77. if (pdata == NULL) {
  78. pr_err("%s: no pdata\n", __func__);
  79. return -ENODEV;
  80. }
  81. hap_data = kzalloc(sizeof(struct max77828_haptic_data), GFP_KERNEL);
  82. if (!hap_data)
  83. return -ENOMEM;
  84. g_hap_data = hap_data;
  85. g_hap_data->reg = MOTOR_LRA | DIVIDER_128;
  86. hap_data->max77828 = max77828;
  87. hap_data->i2c = max77828->i2c;
  88. hap_data->pdata = pdata;
  89. platform_set_drvdata(pdev, hap_data);
  90. max77828_haptic_i2c(hap_data, true);
  91. spin_lock_init(&(hap_data->lock));
  92. pr_err("[VIB] -- %s\n", __func__);
  93. return error;
  94. }
  95. static int __devexit max77828_haptic_remove(struct platform_device *pdev)
  96. {
  97. struct max77828_haptic_data *data = platform_get_drvdata(pdev);
  98. kfree(data);
  99. g_hap_data = NULL;
  100. return 0;
  101. }
  102. static void max77828_haptic_shutdown(struct device *dev)
  103. {
  104. struct max77828_haptic_data *data = dev_get_drvdata(dev);
  105. int ret;
  106. pr_info("%s: Disable HAPTIC\n", __func__);
  107. ret = max77828_update_reg(data->i2c, MAX77828_PMIC_REG_MCONFIG, 0x0, MOTOR_EN);
  108. if (ret < 0) {
  109. pr_err("%s: fail to update reg\n", __func__);
  110. return;
  111. }
  112. }
  113. static int max77828_haptic_suspend(struct platform_device *pdev,
  114. pm_message_t state)
  115. {
  116. return 0;
  117. }
  118. static int max77828_haptic_resume(struct platform_device *pdev)
  119. {
  120. return 0;
  121. }
  122. static struct platform_driver max77828_haptic_driver = {
  123. .probe = max77828_haptic_probe,
  124. .remove = max77828_haptic_remove,
  125. .suspend = max77828_haptic_suspend,
  126. .resume = max77828_haptic_resume,
  127. .driver = {
  128. .name = "max77828-haptic",
  129. .owner = THIS_MODULE,
  130. .shutdown = max77828_haptic_shutdown,
  131. },
  132. };
  133. static int __init max77828_haptic_init(void)
  134. {
  135. pr_debug("[VIB] %s\n", __func__);
  136. return platform_driver_register(&max77828_haptic_driver);
  137. }
  138. module_init(max77828_haptic_init);
  139. static void __exit max77828_haptic_exit(void)
  140. {
  141. platform_driver_unregister(&max77828_haptic_driver);
  142. }
  143. module_exit(max77828_haptic_exit);
  144. MODULE_AUTHOR("Ravi Shekhar Singh <shekhar.sr@samsung.com>");
  145. MODULE_LICENSE("GPL");
  146. MODULE_DESCRIPTION("max77828 haptic driver");