cma3000_d0x.c 9.3 KB

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