as5011.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) 2010, 2011 Fabien Marteau <fabien.marteau@armadeus.com>
  3. * Sponsored by ARMadeus Systems
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Driver for Austria Microsystems joysticks AS5011
  20. *
  21. * TODO:
  22. * - Power on the chip when open() and power down when close()
  23. * - Manage power mode
  24. */
  25. #include <linux/i2c.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/input.h>
  28. #include <linux/gpio.h>
  29. #include <linux/delay.h>
  30. #include <linux/input/as5011.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #define DRIVER_DESC "Driver for Austria Microsystems AS5011 joystick"
  34. #define MODULE_DEVICE_ALIAS "as5011"
  35. MODULE_AUTHOR("Fabien Marteau <fabien.marteau@armadeus.com>");
  36. MODULE_DESCRIPTION(DRIVER_DESC);
  37. MODULE_LICENSE("GPL");
  38. /* registers */
  39. #define AS5011_CTRL1 0x76
  40. #define AS5011_CTRL2 0x75
  41. #define AS5011_XP 0x43
  42. #define AS5011_XN 0x44
  43. #define AS5011_YP 0x53
  44. #define AS5011_YN 0x54
  45. #define AS5011_X_REG 0x41
  46. #define AS5011_Y_REG 0x42
  47. #define AS5011_X_RES_INT 0x51
  48. #define AS5011_Y_RES_INT 0x52
  49. /* CTRL1 bits */
  50. #define AS5011_CTRL1_LP_PULSED 0x80
  51. #define AS5011_CTRL1_LP_ACTIVE 0x40
  52. #define AS5011_CTRL1_LP_CONTINUE 0x20
  53. #define AS5011_CTRL1_INT_WUP_EN 0x10
  54. #define AS5011_CTRL1_INT_ACT_EN 0x08
  55. #define AS5011_CTRL1_EXT_CLK_EN 0x04
  56. #define AS5011_CTRL1_SOFT_RST 0x02
  57. #define AS5011_CTRL1_DATA_VALID 0x01
  58. /* CTRL2 bits */
  59. #define AS5011_CTRL2_EXT_SAMPLE_EN 0x08
  60. #define AS5011_CTRL2_RC_BIAS_ON 0x04
  61. #define AS5011_CTRL2_INV_SPINNING 0x02
  62. #define AS5011_MAX_AXIS 80
  63. #define AS5011_MIN_AXIS (-80)
  64. #define AS5011_FUZZ 8
  65. #define AS5011_FLAT 40
  66. struct as5011_device {
  67. struct input_dev *input_dev;
  68. struct i2c_client *i2c_client;
  69. unsigned int button_gpio;
  70. unsigned int button_irq;
  71. unsigned int axis_irq;
  72. };
  73. static int as5011_i2c_write(struct i2c_client *client,
  74. uint8_t aregaddr,
  75. uint8_t avalue)
  76. {
  77. uint8_t data[2] = { aregaddr, avalue };
  78. struct i2c_msg msg = {
  79. client->addr, I2C_M_IGNORE_NAK, 2, (uint8_t *)data
  80. };
  81. int error;
  82. error = i2c_transfer(client->adapter, &msg, 1);
  83. return error < 0 ? error : 0;
  84. }
  85. static int as5011_i2c_read(struct i2c_client *client,
  86. uint8_t aregaddr, signed char *value)
  87. {
  88. uint8_t data[2] = { aregaddr };
  89. struct i2c_msg msg_set[2] = {
  90. { client->addr, I2C_M_REV_DIR_ADDR, 1, (uint8_t *)data },
  91. { client->addr, I2C_M_RD | I2C_M_NOSTART, 1, (uint8_t *)data }
  92. };
  93. int error;
  94. error = i2c_transfer(client->adapter, msg_set, 2);
  95. if (error < 0)
  96. return error;
  97. *value = data[0] & 0x80 ? -1 * (1 + ~data[0]) : data[0];
  98. return 0;
  99. }
  100. static irqreturn_t as5011_button_interrupt(int irq, void *dev_id)
  101. {
  102. struct as5011_device *as5011 = dev_id;
  103. int val = gpio_get_value_cansleep(as5011->button_gpio);
  104. input_report_key(as5011->input_dev, BTN_JOYSTICK, !val);
  105. input_sync(as5011->input_dev);
  106. return IRQ_HANDLED;
  107. }
  108. static irqreturn_t as5011_axis_interrupt(int irq, void *dev_id)
  109. {
  110. struct as5011_device *as5011 = dev_id;
  111. int error;
  112. signed char x, y;
  113. error = as5011_i2c_read(as5011->i2c_client, AS5011_X_RES_INT, &x);
  114. if (error < 0)
  115. goto out;
  116. error = as5011_i2c_read(as5011->i2c_client, AS5011_Y_RES_INT, &y);
  117. if (error < 0)
  118. goto out;
  119. input_report_abs(as5011->input_dev, ABS_X, x);
  120. input_report_abs(as5011->input_dev, ABS_Y, y);
  121. input_sync(as5011->input_dev);
  122. out:
  123. return IRQ_HANDLED;
  124. }
  125. static int __devinit as5011_configure_chip(struct as5011_device *as5011,
  126. const struct as5011_platform_data *plat_dat)
  127. {
  128. struct i2c_client *client = as5011->i2c_client;
  129. int error;
  130. signed char value;
  131. /* chip soft reset */
  132. error = as5011_i2c_write(client, AS5011_CTRL1,
  133. AS5011_CTRL1_SOFT_RST);
  134. if (error < 0) {
  135. dev_err(&client->dev, "Soft reset failed\n");
  136. return error;
  137. }
  138. mdelay(10);
  139. error = as5011_i2c_write(client, AS5011_CTRL1,
  140. AS5011_CTRL1_LP_PULSED |
  141. AS5011_CTRL1_LP_ACTIVE |
  142. AS5011_CTRL1_INT_ACT_EN);
  143. if (error < 0) {
  144. dev_err(&client->dev, "Power config failed\n");
  145. return error;
  146. }
  147. error = as5011_i2c_write(client, AS5011_CTRL2,
  148. AS5011_CTRL2_INV_SPINNING);
  149. if (error < 0) {
  150. dev_err(&client->dev, "Can't invert spinning\n");
  151. return error;
  152. }
  153. /* write threshold */
  154. error = as5011_i2c_write(client, AS5011_XP, plat_dat->xp);
  155. if (error < 0) {
  156. dev_err(&client->dev, "Can't write threshold\n");
  157. return error;
  158. }
  159. error = as5011_i2c_write(client, AS5011_XN, plat_dat->xn);
  160. if (error < 0) {
  161. dev_err(&client->dev, "Can't write threshold\n");
  162. return error;
  163. }
  164. error = as5011_i2c_write(client, AS5011_YP, plat_dat->yp);
  165. if (error < 0) {
  166. dev_err(&client->dev, "Can't write threshold\n");
  167. return error;
  168. }
  169. error = as5011_i2c_write(client, AS5011_YN, plat_dat->yn);
  170. if (error < 0) {
  171. dev_err(&client->dev, "Can't write threshold\n");
  172. return error;
  173. }
  174. /* to free irq gpio in chip */
  175. error = as5011_i2c_read(client, AS5011_X_RES_INT, &value);
  176. if (error < 0) {
  177. dev_err(&client->dev, "Can't read i2c X resolution value\n");
  178. return error;
  179. }
  180. return 0;
  181. }
  182. static int __devinit as5011_probe(struct i2c_client *client,
  183. const struct i2c_device_id *id)
  184. {
  185. const struct as5011_platform_data *plat_data;
  186. struct as5011_device *as5011;
  187. struct input_dev *input_dev;
  188. int irq;
  189. int error;
  190. plat_data = client->dev.platform_data;
  191. if (!plat_data)
  192. return -EINVAL;
  193. if (!plat_data->axis_irq) {
  194. dev_err(&client->dev, "No axis IRQ?\n");
  195. return -EINVAL;
  196. }
  197. if (!i2c_check_functionality(client->adapter,
  198. I2C_FUNC_PROTOCOL_MANGLING)) {
  199. dev_err(&client->dev,
  200. "need i2c bus that supports protocol mangling\n");
  201. return -ENODEV;
  202. }
  203. as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
  204. input_dev = input_allocate_device();
  205. if (!as5011 || !input_dev) {
  206. dev_err(&client->dev,
  207. "Can't allocate memory for device structure\n");
  208. error = -ENOMEM;
  209. goto err_free_mem;
  210. }
  211. as5011->i2c_client = client;
  212. as5011->input_dev = input_dev;
  213. as5011->button_gpio = plat_data->button_gpio;
  214. as5011->axis_irq = plat_data->axis_irq;
  215. input_dev->name = "Austria Microsystem as5011 joystick";
  216. input_dev->id.bustype = BUS_I2C;
  217. input_dev->dev.parent = &client->dev;
  218. __set_bit(EV_KEY, input_dev->evbit);
  219. __set_bit(EV_ABS, input_dev->evbit);
  220. __set_bit(BTN_JOYSTICK, input_dev->keybit);
  221. input_set_abs_params(input_dev, ABS_X,
  222. AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
  223. input_set_abs_params(as5011->input_dev, ABS_Y,
  224. AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
  225. error = gpio_request(as5011->button_gpio, "AS5011 button");
  226. if (error < 0) {
  227. dev_err(&client->dev, "Failed to request button gpio\n");
  228. goto err_free_mem;
  229. }
  230. irq = gpio_to_irq(as5011->button_gpio);
  231. if (irq < 0) {
  232. dev_err(&client->dev,
  233. "Failed to get irq number for button gpio\n");
  234. goto err_free_button_gpio;
  235. }
  236. as5011->button_irq = irq;
  237. error = request_threaded_irq(as5011->button_irq,
  238. NULL, as5011_button_interrupt,
  239. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  240. "as5011_button", as5011);
  241. if (error < 0) {
  242. dev_err(&client->dev,
  243. "Can't allocate button irq %d\n", as5011->button_irq);
  244. goto err_free_button_gpio;
  245. }
  246. error = as5011_configure_chip(as5011, plat_data);
  247. if (error)
  248. goto err_free_button_irq;
  249. error = request_threaded_irq(as5011->axis_irq, NULL,
  250. as5011_axis_interrupt,
  251. plat_data->axis_irqflags,
  252. "as5011_joystick", as5011);
  253. if (error) {
  254. dev_err(&client->dev,
  255. "Can't allocate axis irq %d\n", plat_data->axis_irq);
  256. goto err_free_button_irq;
  257. }
  258. error = input_register_device(as5011->input_dev);
  259. if (error) {
  260. dev_err(&client->dev, "Failed to register input device\n");
  261. goto err_free_axis_irq;
  262. }
  263. i2c_set_clientdata(client, as5011);
  264. return 0;
  265. err_free_axis_irq:
  266. free_irq(as5011->axis_irq, as5011);
  267. err_free_button_irq:
  268. free_irq(as5011->button_irq, as5011);
  269. err_free_button_gpio:
  270. gpio_free(as5011->button_gpio);
  271. err_free_mem:
  272. input_free_device(input_dev);
  273. kfree(as5011);
  274. return error;
  275. }
  276. static int __devexit as5011_remove(struct i2c_client *client)
  277. {
  278. struct as5011_device *as5011 = i2c_get_clientdata(client);
  279. free_irq(as5011->axis_irq, as5011);
  280. free_irq(as5011->button_irq, as5011);
  281. gpio_free(as5011->button_gpio);
  282. input_unregister_device(as5011->input_dev);
  283. kfree(as5011);
  284. return 0;
  285. }
  286. static const struct i2c_device_id as5011_id[] = {
  287. { MODULE_DEVICE_ALIAS, 0 },
  288. { }
  289. };
  290. MODULE_DEVICE_TABLE(i2c, as5011_id);
  291. static struct i2c_driver as5011_driver = {
  292. .driver = {
  293. .name = "as5011",
  294. },
  295. .probe = as5011_probe,
  296. .remove = __devexit_p(as5011_remove),
  297. .id_table = as5011_id,
  298. };
  299. module_i2c_driver(as5011_driver);