cma3000_d0x.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * VTI CMA3000_D0x Accelerometer driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. * Author: Hemanth V <hemanthv@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/types.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/input.h>
  24. #include <linux/input/cma3000.h>
  25. #include <linux/module.h>
  26. #include "cma3000_d0x.h"
  27. #define CMA3000_WHOAMI 0x00
  28. #define CMA3000_REVID 0x01
  29. #define CMA3000_CTRL 0x02
  30. #define CMA3000_STATUS 0x03
  31. #define CMA3000_RSTR 0x04
  32. #define CMA3000_INTSTATUS 0x05
  33. #define CMA3000_DOUTX 0x06
  34. #define CMA3000_DOUTY 0x07
  35. #define CMA3000_DOUTZ 0x08
  36. #define CMA3000_MDTHR 0x09
  37. #define CMA3000_MDFFTMR 0x0A
  38. #define CMA3000_FFTHR 0x0B
  39. #define CMA3000_RANGE2G (1 << 7)
  40. #define CMA3000_RANGE8G (0 << 7)
  41. #define CMA3000_BUSI2C (0 << 4)
  42. #define CMA3000_MODEMASK (7 << 1)
  43. #define CMA3000_GRANGEMASK (1 << 7)
  44. #define CMA3000_STATUS_PERR 1
  45. #define CMA3000_INTSTATUS_FFDET (1 << 2)
  46. /* Settling time delay in ms */
  47. #define CMA3000_SETDELAY 30
  48. /* Delay for clearing interrupt in us */
  49. #define CMA3000_INTDELAY 44
  50. /*
  51. * Bit weights in mg for bit 0, other bits need
  52. * multiply factor 2^n. Eight bit is the sign bit.
  53. */
  54. #define BIT_TO_2G 18
  55. #define BIT_TO_8G 71
  56. struct cma3000_accl_data {
  57. const struct cma3000_bus_ops *bus_ops;
  58. const struct cma3000_platform_data *pdata;
  59. struct device *dev;
  60. struct input_dev *input_dev;
  61. int bit_to_mg;
  62. int irq;
  63. int g_range;
  64. u8 mode;
  65. struct mutex mutex;
  66. bool opened;
  67. bool suspended;
  68. };
  69. #define CMA3000_READ(data, reg, msg) \
  70. (data->bus_ops->read(data->dev, reg, msg))
  71. #define CMA3000_SET(data, reg, val, msg) \
  72. ((data)->bus_ops->write(data->dev, reg, val, msg))
  73. /*
  74. * Conversion for each of the eight modes to g, depending
  75. * on G range i.e 2G or 8G. Some modes always operate in
  76. * 8G.
  77. */
  78. static int mode_to_mg[8][2] = {
  79. { 0, 0 },
  80. { BIT_TO_8G, BIT_TO_2G },
  81. { BIT_TO_8G, BIT_TO_2G },
  82. { BIT_TO_8G, BIT_TO_8G },
  83. { BIT_TO_8G, BIT_TO_8G },
  84. { BIT_TO_8G, BIT_TO_2G },
  85. { BIT_TO_8G, BIT_TO_2G },
  86. { 0, 0},
  87. };
  88. static void decode_mg(struct cma3000_accl_data *data, int *datax,
  89. int *datay, int *dataz)
  90. {
  91. /* Data in 2's complement, convert to mg */
  92. *datax = ((s8)*datax) * data->bit_to_mg;
  93. *datay = ((s8)*datay) * data->bit_to_mg;
  94. *dataz = ((s8)*dataz) * data->bit_to_mg;
  95. }
  96. static irqreturn_t cma3000_thread_irq(int irq, void *dev_id)
  97. {
  98. struct cma3000_accl_data *data = dev_id;
  99. int datax, datay, dataz, intr_status;
  100. u8 ctrl, mode, range;
  101. intr_status = CMA3000_READ(data, CMA3000_INTSTATUS, "interrupt status");
  102. if (intr_status < 0)
  103. return IRQ_NONE;
  104. /* Check if free fall is detected, report immediately */
  105. if (intr_status & CMA3000_INTSTATUS_FFDET) {
  106. input_report_abs(data->input_dev, ABS_MISC, 1);
  107. input_sync(data->input_dev);
  108. } else {
  109. input_report_abs(data->input_dev, ABS_MISC, 0);
  110. }
  111. datax = CMA3000_READ(data, CMA3000_DOUTX, "X");
  112. datay = CMA3000_READ(data, CMA3000_DOUTY, "Y");
  113. dataz = CMA3000_READ(data, CMA3000_DOUTZ, "Z");
  114. ctrl = CMA3000_READ(data, CMA3000_CTRL, "ctrl");
  115. mode = (ctrl & CMA3000_MODEMASK) >> 1;
  116. range = (ctrl & CMA3000_GRANGEMASK) >> 7;
  117. data->bit_to_mg = mode_to_mg[mode][range];
  118. /* Interrupt not for this device */
  119. if (data->bit_to_mg == 0)
  120. return IRQ_NONE;
  121. /* Decode register values to milli g */
  122. decode_mg(data, &datax, &datay, &dataz);
  123. input_report_abs(data->input_dev, ABS_X, datax);
  124. input_report_abs(data->input_dev, ABS_Y, datay);
  125. input_report_abs(data->input_dev, ABS_Z, dataz);
  126. input_sync(data->input_dev);
  127. return IRQ_HANDLED;
  128. }
  129. static int cma3000_reset(struct cma3000_accl_data *data)
  130. {
  131. int val;
  132. /* Reset sequence */
  133. CMA3000_SET(data, CMA3000_RSTR, 0x02, "Reset");
  134. CMA3000_SET(data, CMA3000_RSTR, 0x0A, "Reset");
  135. CMA3000_SET(data, CMA3000_RSTR, 0x04, "Reset");
  136. /* Settling time delay */
  137. mdelay(10);
  138. val = CMA3000_READ(data, CMA3000_STATUS, "Status");
  139. if (val < 0) {
  140. dev_err(data->dev, "Reset failed\n");
  141. return val;
  142. }
  143. if (val & CMA3000_STATUS_PERR) {
  144. dev_err(data->dev, "Parity Error\n");
  145. return -EIO;
  146. }
  147. return 0;
  148. }
  149. static int cma3000_poweron(struct cma3000_accl_data *data)
  150. {
  151. const struct cma3000_platform_data *pdata = data->pdata;
  152. u8 ctrl = 0;
  153. int ret;
  154. if (data->g_range == CMARANGE_2G) {
  155. ctrl = (data->mode << 1) | CMA3000_RANGE2G;
  156. } else if (data->g_range == CMARANGE_8G) {
  157. ctrl = (data->mode << 1) | CMA3000_RANGE8G;
  158. } else {
  159. dev_info(data->dev,
  160. "Invalid G range specified, assuming 8G\n");
  161. ctrl = (data->mode << 1) | CMA3000_RANGE8G;
  162. }
  163. ctrl |= data->bus_ops->ctrl_mod;
  164. CMA3000_SET(data, CMA3000_MDTHR, pdata->mdthr,
  165. "Motion Detect Threshold");
  166. CMA3000_SET(data, CMA3000_MDFFTMR, pdata->mdfftmr,
  167. "Time register");
  168. CMA3000_SET(data, CMA3000_FFTHR, pdata->ffthr,
  169. "Free fall threshold");
  170. ret = CMA3000_SET(data, CMA3000_CTRL, ctrl, "Mode setting");
  171. if (ret < 0)
  172. return -EIO;
  173. msleep(CMA3000_SETDELAY);
  174. return 0;
  175. }
  176. static int cma3000_poweroff(struct cma3000_accl_data *data)
  177. {
  178. int ret;
  179. ret = CMA3000_SET(data, CMA3000_CTRL, CMAMODE_POFF, "Mode setting");
  180. msleep(CMA3000_SETDELAY);
  181. return ret;
  182. }
  183. static int cma3000_open(struct input_dev *input_dev)
  184. {
  185. struct cma3000_accl_data *data = input_get_drvdata(input_dev);
  186. mutex_lock(&data->mutex);
  187. if (!data->suspended)
  188. cma3000_poweron(data);
  189. data->opened = true;
  190. mutex_unlock(&data->mutex);
  191. return 0;
  192. }
  193. static void cma3000_close(struct input_dev *input_dev)
  194. {
  195. struct cma3000_accl_data *data = input_get_drvdata(input_dev);
  196. mutex_lock(&data->mutex);
  197. if (!data->suspended)
  198. cma3000_poweroff(data);
  199. data->opened = false;
  200. mutex_unlock(&data->mutex);
  201. }
  202. void cma3000_suspend(struct cma3000_accl_data *data)
  203. {
  204. mutex_lock(&data->mutex);
  205. if (!data->suspended && data->opened)
  206. cma3000_poweroff(data);
  207. data->suspended = true;
  208. mutex_unlock(&data->mutex);
  209. }
  210. EXPORT_SYMBOL(cma3000_suspend);
  211. void cma3000_resume(struct cma3000_accl_data *data)
  212. {
  213. mutex_lock(&data->mutex);
  214. if (data->suspended && data->opened)
  215. cma3000_poweron(data);
  216. data->suspended = false;
  217. mutex_unlock(&data->mutex);
  218. }
  219. EXPORT_SYMBOL(cma3000_resume);
  220. struct cma3000_accl_data *cma3000_init(struct device *dev, int irq,
  221. const struct cma3000_bus_ops *bops)
  222. {
  223. const struct cma3000_platform_data *pdata = dev_get_platdata(dev);
  224. struct cma3000_accl_data *data;
  225. struct input_dev *input_dev;
  226. int rev;
  227. int error;
  228. if (!pdata) {
  229. dev_err(dev, "platform data not found\n");
  230. error = -EINVAL;
  231. goto err_out;
  232. }
  233. /* if no IRQ return error */
  234. if (irq == 0) {
  235. error = -EINVAL;
  236. goto err_out;
  237. }
  238. data = kzalloc(sizeof(struct cma3000_accl_data), GFP_KERNEL);
  239. input_dev = input_allocate_device();
  240. if (!data || !input_dev) {
  241. error = -ENOMEM;
  242. goto err_free_mem;
  243. }
  244. data->dev = dev;
  245. data->input_dev = input_dev;
  246. data->bus_ops = bops;
  247. data->pdata = pdata;
  248. data->irq = irq;
  249. mutex_init(&data->mutex);
  250. data->mode = pdata->mode;
  251. if (data->mode > CMAMODE_POFF) {
  252. data->mode = CMAMODE_MOTDET;
  253. dev_warn(dev,
  254. "Invalid mode specified, assuming Motion Detect\n");
  255. }
  256. data->g_range = pdata->g_range;
  257. if (data->g_range != CMARANGE_2G && data->g_range != CMARANGE_8G) {
  258. dev_info(dev,
  259. "Invalid G range specified, assuming 8G\n");
  260. data->g_range = CMARANGE_8G;
  261. }
  262. input_dev->name = "cma3000-accelerometer";
  263. input_dev->id.bustype = bops->bustype;
  264. input_dev->open = cma3000_open;
  265. input_dev->close = cma3000_close;
  266. __set_bit(EV_ABS, input_dev->evbit);
  267. input_set_abs_params(input_dev, ABS_X,
  268. -data->g_range, data->g_range, pdata->fuzz_x, 0);
  269. input_set_abs_params(input_dev, ABS_Y,
  270. -data->g_range, data->g_range, pdata->fuzz_y, 0);
  271. input_set_abs_params(input_dev, ABS_Z,
  272. -data->g_range, data->g_range, pdata->fuzz_z, 0);
  273. input_set_abs_params(input_dev, ABS_MISC, 0, 1, 0, 0);
  274. input_set_drvdata(input_dev, data);
  275. error = cma3000_reset(data);
  276. if (error)
  277. goto err_free_mem;
  278. rev = CMA3000_READ(data, CMA3000_REVID, "Revid");
  279. if (rev < 0) {
  280. error = rev;
  281. goto err_free_mem;
  282. }
  283. pr_info("CMA3000 Accelerometer: Revision %x\n", rev);
  284. error = request_threaded_irq(irq, NULL, cma3000_thread_irq,
  285. pdata->irqflags | IRQF_ONESHOT,
  286. "cma3000_d0x", data);
  287. if (error) {
  288. dev_err(dev, "request_threaded_irq failed\n");
  289. goto err_free_mem;
  290. }
  291. error = input_register_device(data->input_dev);
  292. if (error) {
  293. dev_err(dev, "Unable to register input device\n");
  294. goto err_free_irq;
  295. }
  296. return data;
  297. err_free_irq:
  298. free_irq(irq, data);
  299. err_free_mem:
  300. input_free_device(input_dev);
  301. kfree(data);
  302. err_out:
  303. return ERR_PTR(error);
  304. }
  305. EXPORT_SYMBOL(cma3000_init);
  306. void cma3000_exit(struct cma3000_accl_data *data)
  307. {
  308. free_irq(data->irq, data);
  309. input_unregister_device(data->input_dev);
  310. kfree(data);
  311. }
  312. EXPORT_SYMBOL(cma3000_exit);
  313. MODULE_DESCRIPTION("CMA3000-D0x Accelerometer Driver");
  314. MODULE_LICENSE("GPL");
  315. MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");