isa1400_vibrator.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* drivers/motor/isa1400_vibrator.c
  2. *
  3. * Copyright (C) 2011 Samsung Electronics Co. Ltd. All Rights Reserved.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/hrtimer.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/wakelock.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pwm.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/fs.h>
  27. #include <linux/mm.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include <linux/fs.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/isa1400_vibrator.h>
  34. #include "../staging/android/timed_output.h"
  35. #include "ss_vibrator.h"
  36. #define MOTOR_DEBUG 0
  37. static int vib_work_lock = 0;
  38. static struct wake_lock vib_wake_lock;
  39. struct isa1400_vibrator_drvdata {
  40. struct timed_output_dev dev;
  41. struct hrtimer timer;
  42. struct work_struct work;
  43. struct pwm_device *pwm;
  44. struct i2c_client *client;
  45. struct isa1400_vibrator_platform_data *pdata;
  46. struct mutex mutex_lock;
  47. bool running;
  48. int timeout;
  49. int intensity;
  50. };
  51. static int isa1400_vibrator_i2c_write(struct i2c_client *client,
  52. u8 addr, u8 val)
  53. {
  54. int error = 0;
  55. error = i2c_smbus_write_byte_data(client, addr, val);
  56. if (error)
  57. printk(KERN_ERR "[VIB] Failed to write addr=[0x%x], val=[0x%x]\n",
  58. addr, val);
  59. #if MOTOR_DEBUG
  60. printk(KERN_DEBUG "[VIB] %s : 0x%x, 0x%x\n",
  61. __func__, addr, val);
  62. #endif
  63. return error;
  64. }
  65. static void isa1400_reg_write(struct isa1400_vibrator_drvdata *data,
  66. u8 type)
  67. {
  68. int i = 0, j = 0;
  69. for (i = 0; i < ISA1400_REG_MAX; i++) {
  70. if (data->pdata->reg_data[i][0] == type){
  71. for (j = 2; j < data->pdata->reg_data[i][1]; j += 2){
  72. /*printk("[VIB] inside loop %d %d %d\n", j,
  73. data->pdata->reg_data[i][j],
  74. data->pdata->reg_data[i][j + 1]);*/
  75. isa1400_vibrator_i2c_write(data->client,
  76. data->pdata->reg_data[i][j],
  77. data->pdata->reg_data[i][j + 1]);
  78. }
  79. }
  80. }
  81. }
  82. static void isa1400_vibrator_hw_init(struct isa1400_vibrator_drvdata *data)
  83. {
  84. data->pdata->gpio_en(true);
  85. data->pdata->clk_en(true);
  86. isa1400_reg_write(data, ISA1400_REG_INIT);
  87. }
  88. static void isa1400_vibrator_on(struct isa1400_vibrator_drvdata *data)
  89. {
  90. isa1400_vibrator_hw_init(data);
  91. isa1400_reg_write(data, ISA1400_REG_START);
  92. }
  93. static void isa1400_vibrator_off(struct isa1400_vibrator_drvdata *data)
  94. {
  95. if(vib_work_lock)
  96. return;
  97. isa1400_reg_write(data, ISA1400_REG_STOP);
  98. data->pdata->clk_en(false);
  99. data->pdata->gpio_en(false);
  100. }
  101. static struct isa1400_vibrator_drvdata *g_drvdata;
  102. int isa1400_i2c_write(u8 addr, int length, u8 *data)
  103. {
  104. if (NULL == g_drvdata) {
  105. printk(KERN_ERR "[VIB] driver is not ready\n");
  106. return -EFAULT;
  107. }
  108. if (2 != length)
  109. printk(KERN_ERR "[VIB] length should be 2(len:%d)\n",
  110. length);
  111. #if MOTOR_DEBUG
  112. printk(KERN_DEBUG "[VIB] data : %x, %x\n",
  113. data[0], data[1]);
  114. #endif
  115. return isa1400_vibrator_i2c_write(g_drvdata->client,
  116. data[0], data[1]);
  117. }
  118. void isa1400_clk_config(int duty)
  119. {
  120. if (NULL == g_drvdata) {
  121. printk(KERN_ERR "[VIB] driver is not ready\n");
  122. return ;
  123. }
  124. #if MOTOR_DEBUG
  125. printk(KERN_DEBUG "[VIB] %s duty %d\n", __func__, duty);
  126. #endif
  127. duty = (duty > 0) ? duty : (abs(duty) | 0x80);
  128. isa1400_vibrator_i2c_write(g_drvdata->client,
  129. ISA1400_REG_GAIN, duty);
  130. isa1400_vibrator_i2c_write(g_drvdata->client,
  131. ISA1400_REG_HPTEN, 0x01);
  132. }
  133. void vibe_set_intensity(int intensity)
  134. {
  135. if (!g_drvdata->running) {
  136. g_drvdata->pdata->gpio_en(true);
  137. }
  138. if (0 == intensity)
  139. isa1400_clk_config(intensity);
  140. else {
  141. if (MAX_INTENSITY == intensity)
  142. intensity = 127;
  143. else if (0 != intensity) {
  144. int tmp = intensity;
  145. intensity = (tmp / 79); // 79 := 10000 / 127
  146. }
  147. isa1400_clk_config(intensity);
  148. }
  149. }
  150. static enum hrtimer_restart isa1400_vibrator_timer_func(struct hrtimer *_timer)
  151. {
  152. struct isa1400_vibrator_drvdata *data =
  153. container_of(_timer, struct isa1400_vibrator_drvdata, timer);
  154. data->timeout = 0;
  155. schedule_work(&data->work);
  156. return HRTIMER_NORESTART;
  157. }
  158. static void isa1400_vibrator_work(struct work_struct *_work)
  159. {
  160. struct isa1400_vibrator_drvdata *data =
  161. container_of(_work, struct isa1400_vibrator_drvdata, work);
  162. if (0 == data->timeout) {
  163. if (!data->running)
  164. return ;
  165. data->running = false;
  166. isa1400_vibrator_off(data);
  167. } else {
  168. if (data->running)
  169. return ;
  170. data->running = true;
  171. isa1400_vibrator_on(data);
  172. vibe_set_intensity(data->intensity);
  173. }
  174. }
  175. static int isa1400_vibrator_get_time(struct timed_output_dev *_dev)
  176. {
  177. struct isa1400_vibrator_drvdata *data =
  178. container_of(_dev, struct isa1400_vibrator_drvdata, dev);
  179. if (hrtimer_active(&data->timer)) {
  180. ktime_t r = hrtimer_get_remaining(&data->timer);
  181. struct timeval t = ktime_to_timeval(r);
  182. return t.tv_sec * 1000 + t.tv_usec / 1000;
  183. } else
  184. return 0;
  185. }
  186. static void isa1400_vibrator_enable(struct timed_output_dev *_dev, int value)
  187. {
  188. struct isa1400_vibrator_drvdata *data =
  189. container_of(_dev, struct isa1400_vibrator_drvdata, dev);
  190. printk(KERN_DEBUG "[VIB] time = %dms\n", value);
  191. mutex_lock(&data->mutex_lock);
  192. cancel_work_sync(&data->work);
  193. hrtimer_cancel(&data->timer);
  194. data->timeout = value;
  195. if (value > 0) {
  196. if (value > data->pdata->max_timeout)
  197. value = data->pdata->max_timeout;
  198. hrtimer_start(&data->timer,
  199. ns_to_ktime((u64)value * NSEC_PER_MSEC),
  200. HRTIMER_MODE_REL);
  201. }
  202. mutex_unlock(&data->mutex_lock);
  203. schedule_work(&data->work);
  204. }
  205. static ssize_t intensity_store(struct device *dev,
  206. struct device_attribute *devattr, const char *buf, size_t count)
  207. {
  208. int ret = 0, set_intensity = 0;
  209. ret = kstrtoint(buf, 0, &set_intensity);
  210. if ((set_intensity < 0) || (set_intensity > MAX_INTENSITY)) {
  211. pr_err("[VIB]: %sout of rage\n", __func__);
  212. return -EINVAL;
  213. }
  214. vibe_set_intensity(set_intensity);
  215. g_drvdata->intensity = set_intensity;
  216. return count;
  217. }
  218. static ssize_t intensity_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. return sprintf(buf, "intensity: %u\n", g_drvdata->intensity);
  222. }
  223. static DEVICE_ATTR(intensity, 0660, intensity_show, intensity_store);
  224. static int __devinit isa1400_vibrator_i2c_probe(struct i2c_client *client,
  225. const struct i2c_device_id *id)
  226. {
  227. struct isa1400_vibrator_platform_data *pdata =
  228. client->dev.platform_data;
  229. struct isa1400_vibrator_drvdata *ddata;
  230. int ret = 0;
  231. ddata = kzalloc(sizeof(struct isa1400_vibrator_drvdata), GFP_KERNEL);
  232. if (NULL == ddata) {
  233. printk(KERN_ERR "[VIB] Failed to alloc memory.\n");
  234. ret = -ENOMEM;
  235. goto err_free_mem;
  236. }
  237. ddata->client = client;
  238. if (NULL == pdata) {
  239. printk(KERN_ERR "[VIB] the platform data is empty.\n");
  240. goto err_platform_data;
  241. } else
  242. ddata->pdata = pdata;
  243. ddata->dev.name = "vibrator";
  244. ddata->dev.get_time = isa1400_vibrator_get_time;
  245. ddata->dev.enable = isa1400_vibrator_enable;
  246. hrtimer_init(&ddata->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  247. ddata->timer.function = isa1400_vibrator_timer_func;
  248. INIT_WORK(&ddata->work, isa1400_vibrator_work);
  249. mutex_init(&ddata->mutex_lock);
  250. ddata->intensity = MAX_INTENSITY;
  251. i2c_set_clientdata(client, ddata);
  252. /*ddata->pdata->gpio_en(true);
  253. isa1400_vibrator_hw_init(ddata);*/
  254. wake_lock_init(&vib_wake_lock, WAKE_LOCK_SUSPEND, "vib_present");
  255. ret = timed_output_dev_register(&ddata->dev);
  256. if (ret < 0) {
  257. printk(KERN_ERR "[VIB] Failed to register timed_output : -%d\n", ret);
  258. goto err_to_dev_reg;
  259. }
  260. ret = sysfs_create_file(&ddata->dev.dev->kobj, &dev_attr_intensity.attr);
  261. if (ret < 0) {
  262. pr_err("[VIB]: Failed to register sysfs intensity: %d\n", ret);
  263. }
  264. g_drvdata = ddata;
  265. return 0;
  266. err_to_dev_reg:
  267. err_platform_data:
  268. kfree(ddata);
  269. err_free_mem:
  270. return ret;
  271. }
  272. static int isa1400_vibrator_suspend(struct i2c_client *client, pm_message_t mesg)
  273. {
  274. struct isa1400_vibrator_drvdata *ddata = i2c_get_clientdata(client);
  275. printk(KERN_INFO "[VIB] isa1400_vibrator_suspend called ");
  276. if(vib_work_lock){
  277. vib_work_lock = 0;
  278. ddata->pdata->gpio_en(false);
  279. ddata->pdata->clk_en(false);
  280. }
  281. return 0;
  282. }
  283. static int isa1400_vibrator_resume(struct i2c_client *client)
  284. {
  285. return 0;
  286. }
  287. static const struct i2c_device_id isa1400_vibrator_device_id[] = {
  288. {"isa1400_vibrator", 0},
  289. {}
  290. };
  291. MODULE_DEVICE_TABLE(i2c, isa1400_vibrator_device_id);
  292. static struct i2c_driver isa1400_vibrator_i2c_driver = {
  293. .driver = {
  294. .name = "isa1400_vibrator",
  295. .owner = THIS_MODULE,
  296. },
  297. .probe = isa1400_vibrator_i2c_probe,
  298. .id_table = isa1400_vibrator_device_id,
  299. .suspend = isa1400_vibrator_suspend,
  300. .resume = isa1400_vibrator_resume,
  301. };
  302. static int __init isa1400_vibrator_init(void)
  303. {
  304. printk("[VIB] init called for 1400\n");
  305. return i2c_add_driver(&isa1400_vibrator_i2c_driver);
  306. }
  307. static void __exit isa1400_vibrator_exit(void)
  308. {
  309. wake_lock_destroy(&vib_wake_lock);
  310. mutex_destroy(&g_drvdata->mutex_lock);
  311. timed_output_dev_unregister(&g_drvdata->dev);
  312. i2c_del_driver(&isa1400_vibrator_i2c_driver);
  313. }
  314. module_init(isa1400_vibrator_init);
  315. module_exit(isa1400_vibrator_exit);
  316. MODULE_AUTHOR("Samsung Corporation");
  317. MODULE_DESCRIPTION("Vibrator driver for the isa1400 ic");
  318. MODULE_LICENSE("GPL v2");