as5011.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #define DRIVER_DESC "Driver for Austria Microsystems AS5011 joystick"
  33. #define MODULE_DEVICE_ALIAS "as5011"
  34. MODULE_AUTHOR("Fabien Marteau <fabien.marteau@armadeus.com>");
  35. MODULE_DESCRIPTION(DRIVER_DESC);
  36. MODULE_LICENSE("GPL");
  37. /* registers */
  38. #define AS5011_CTRL1 0x76
  39. #define AS5011_CTRL2 0x75
  40. #define AS5011_XP 0x43
  41. #define AS5011_XN 0x44
  42. #define AS5011_YP 0x53
  43. #define AS5011_YN 0x54
  44. #define AS5011_X_REG 0x41
  45. #define AS5011_Y_REG 0x42
  46. #define AS5011_X_RES_INT 0x51
  47. #define AS5011_Y_RES_INT 0x52
  48. /* CTRL1 bits */
  49. #define AS5011_CTRL1_LP_PULSED 0x80
  50. #define AS5011_CTRL1_LP_ACTIVE 0x40
  51. #define AS5011_CTRL1_LP_CONTINUE 0x20
  52. #define AS5011_CTRL1_INT_WUP_EN 0x10
  53. #define AS5011_CTRL1_INT_ACT_EN 0x08
  54. #define AS5011_CTRL1_EXT_CLK_EN 0x04
  55. #define AS5011_CTRL1_SOFT_RST 0x02
  56. #define AS5011_CTRL1_DATA_VALID 0x01
  57. /* CTRL2 bits */
  58. #define AS5011_CTRL2_EXT_SAMPLE_EN 0x08
  59. #define AS5011_CTRL2_RC_BIAS_ON 0x04
  60. #define AS5011_CTRL2_INV_SPINNING 0x02
  61. #define AS5011_MAX_AXIS 80
  62. #define AS5011_MIN_AXIS (-80)
  63. #define AS5011_FUZZ 8
  64. #define AS5011_FLAT 40
  65. struct as5011_device {
  66. struct input_dev *input_dev;
  67. struct i2c_client *i2c_client;
  68. unsigned int button_gpio;
  69. unsigned int button_irq;
  70. unsigned int axis_irq;
  71. };
  72. static int as5011_i2c_write(struct i2c_client *client,
  73. uint8_t aregaddr,
  74. uint8_t avalue)
  75. {
  76. uint8_t data[2] = { aregaddr, avalue };
  77. struct i2c_msg msg = {
  78. client->addr, I2C_M_IGNORE_NAK, 2, (uint8_t *)data
  79. };
  80. int error;
  81. error = i2c_transfer(client->adapter, &msg, 1);
  82. return error < 0 ? error : 0;
  83. }
  84. static int as5011_i2c_read(struct i2c_client *client,
  85. uint8_t aregaddr, signed char *value)
  86. {
  87. uint8_t data[2] = { aregaddr };
  88. struct i2c_msg msg_set[2] = {
  89. { client->addr, I2C_M_REV_DIR_ADDR, 1, (uint8_t *)data },
  90. { client->addr, I2C_M_RD | I2C_M_NOSTART, 1, (uint8_t *)data }
  91. };
  92. int error;
  93. error = i2c_transfer(client->adapter, msg_set, 2);
  94. if (error < 0)
  95. return error;
  96. *value = data[0] & 0x80 ? -1 * (1 + ~data[0]) : data[0];
  97. return 0;
  98. }
  99. static irqreturn_t as5011_button_interrupt(int irq, void *dev_id)
  100. {
  101. struct as5011_device *as5011 = dev_id;
  102. int val = gpio_get_value_cansleep(as5011->button_gpio);
  103. input_report_key(as5011->input_dev, BTN_JOYSTICK, !val);
  104. input_sync(as5011->input_dev);
  105. return IRQ_HANDLED;
  106. }
  107. static irqreturn_t as5011_axis_interrupt(int irq, void *dev_id)
  108. {
  109. struct as5011_device *as5011 = dev_id;
  110. int error;
  111. signed char x, y;
  112. error = as5011_i2c_read(as5011->i2c_client, AS5011_X_RES_INT, &x);
  113. if (error < 0)
  114. goto out;
  115. error = as5011_i2c_read(as5011->i2c_client, AS5011_Y_RES_INT, &y);
  116. if (error < 0)
  117. goto out;
  118. input_report_abs(as5011->input_dev, ABS_X, x);
  119. input_report_abs(as5011->input_dev, ABS_Y, y);
  120. input_sync(as5011->input_dev);
  121. out:
  122. return IRQ_HANDLED;
  123. }
  124. static int __devinit as5011_configure_chip(struct as5011_device *as5011,
  125. const struct as5011_platform_data *plat_dat)
  126. {
  127. struct i2c_client *client = as5011->i2c_client;
  128. int error;
  129. signed char value;
  130. /* chip soft reset */
  131. error = as5011_i2c_write(client, AS5011_CTRL1,
  132. AS5011_CTRL1_SOFT_RST);
  133. if (error < 0) {
  134. dev_err(&client->dev, "Soft reset failed\n");
  135. return error;
  136. }
  137. mdelay(10);
  138. error = as5011_i2c_write(client, AS5011_CTRL1,
  139. AS5011_CTRL1_LP_PULSED |
  140. AS5011_CTRL1_LP_ACTIVE |
  141. AS5011_CTRL1_INT_ACT_EN);
  142. if (error < 0) {
  143. dev_err(&client->dev, "Power config failed\n");
  144. return error;
  145. }
  146. error = as5011_i2c_write(client, AS5011_CTRL2,
  147. AS5011_CTRL2_INV_SPINNING);
  148. if (error < 0) {
  149. dev_err(&client->dev, "Can't invert spinning\n");
  150. return error;
  151. }
  152. /* write threshold */
  153. error = as5011_i2c_write(client, AS5011_XP, plat_dat->xp);
  154. if (error < 0) {
  155. dev_err(&client->dev, "Can't write threshold\n");
  156. return error;
  157. }
  158. error = as5011_i2c_write(client, AS5011_XN, plat_dat->xn);
  159. if (error < 0) {
  160. dev_err(&client->dev, "Can't write threshold\n");
  161. return error;
  162. }
  163. error = as5011_i2c_write(client, AS5011_YP, plat_dat->yp);
  164. if (error < 0) {
  165. dev_err(&client->dev, "Can't write threshold\n");
  166. return error;
  167. }
  168. error = as5011_i2c_write(client, AS5011_YN, plat_dat->yn);
  169. if (error < 0) {
  170. dev_err(&client->dev, "Can't write threshold\n");
  171. return error;
  172. }
  173. /* to free irq gpio in chip */
  174. error = as5011_i2c_read(client, AS5011_X_RES_INT, &value);
  175. if (error < 0) {
  176. dev_err(&client->dev, "Can't read i2c X resolution value\n");
  177. return error;
  178. }
  179. return 0;
  180. }
  181. static int __devinit as5011_probe(struct i2c_client *client,
  182. const struct i2c_device_id *id)
  183. {
  184. const struct as5011_platform_data *plat_data;
  185. struct as5011_device *as5011;
  186. struct input_dev *input_dev;
  187. int irq;
  188. int error;
  189. plat_data = client->dev.platform_data;
  190. if (!plat_data)
  191. return -EINVAL;
  192. if (!plat_data->axis_irq) {
  193. dev_err(&client->dev, "No axis IRQ?\n");
  194. return -EINVAL;
  195. }
  196. if (!i2c_check_functionality(client->adapter,
  197. I2C_FUNC_PROTOCOL_MANGLING)) {
  198. dev_err(&client->dev,
  199. "need i2c bus that supports protocol mangling\n");
  200. return -ENODEV;
  201. }
  202. as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
  203. input_dev = input_allocate_device();
  204. if (!as5011 || !input_dev) {
  205. dev_err(&client->dev,
  206. "Can't allocate memory for device structure\n");
  207. error = -ENOMEM;
  208. goto err_free_mem;
  209. }
  210. as5011->i2c_client = client;
  211. as5011->input_dev = input_dev;
  212. as5011->button_gpio = plat_data->button_gpio;
  213. as5011->axis_irq = plat_data->axis_irq;
  214. input_dev->name = "Austria Microsystem as5011 joystick";
  215. input_dev->id.bustype = BUS_I2C;
  216. input_dev->dev.parent = &client->dev;
  217. __set_bit(EV_KEY, input_dev->evbit);
  218. __set_bit(EV_ABS, input_dev->evbit);
  219. __set_bit(BTN_JOYSTICK, input_dev->keybit);
  220. input_set_abs_params(input_dev, ABS_X,
  221. AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
  222. input_set_abs_params(as5011->input_dev, ABS_Y,
  223. AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
  224. error = gpio_request(as5011->button_gpio, "AS5011 button");
  225. if (error < 0) {
  226. dev_err(&client->dev, "Failed to request button gpio\n");
  227. goto err_free_mem;
  228. }
  229. irq = gpio_to_irq(as5011->button_gpio);
  230. if (irq < 0) {
  231. dev_err(&client->dev,
  232. "Failed to get irq number for button gpio\n");
  233. goto err_free_button_gpio;
  234. }
  235. as5011->button_irq = irq;
  236. error = request_threaded_irq(as5011->button_irq,
  237. NULL, as5011_button_interrupt,
  238. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  239. "as5011_button", as5011);
  240. if (error < 0) {
  241. dev_err(&client->dev,
  242. "Can't allocate button irq %d\n", as5011->button_irq);
  243. goto err_free_button_gpio;
  244. }
  245. error = as5011_configure_chip(as5011, plat_data);
  246. if (error)
  247. goto err_free_button_irq;
  248. error = request_threaded_irq(as5011->axis_irq, NULL,
  249. as5011_axis_interrupt,
  250. plat_data->axis_irqflags,
  251. "as5011_joystick", as5011);
  252. if (error) {
  253. dev_err(&client->dev,
  254. "Can't allocate axis irq %d\n", plat_data->axis_irq);
  255. goto err_free_button_irq;
  256. }
  257. error = input_register_device(as5011->input_dev);
  258. if (error) {
  259. dev_err(&client->dev, "Failed to register input device\n");
  260. goto err_free_axis_irq;
  261. }
  262. i2c_set_clientdata(client, as5011);
  263. return 0;
  264. err_free_axis_irq:
  265. free_irq(as5011->axis_irq, as5011);
  266. err_free_button_irq:
  267. free_irq(as5011->button_irq, as5011);
  268. err_free_button_gpio:
  269. gpio_free(as5011->button_gpio);
  270. err_free_mem:
  271. input_free_device(input_dev);
  272. kfree(as5011);
  273. return error;
  274. }
  275. static int __devexit as5011_remove(struct i2c_client *client)
  276. {
  277. struct as5011_device *as5011 = i2c_get_clientdata(client);
  278. free_irq(as5011->axis_irq, as5011);
  279. free_irq(as5011->button_irq, as5011);
  280. gpio_free(as5011->button_gpio);
  281. input_unregister_device(as5011->input_dev);
  282. kfree(as5011);
  283. return 0;
  284. }
  285. static const struct i2c_device_id as5011_id[] = {
  286. { MODULE_DEVICE_ALIAS, 0 },
  287. { }
  288. };
  289. MODULE_DEVICE_TABLE(i2c, as5011_id);
  290. static struct i2c_driver as5011_driver = {
  291. .driver = {
  292. .name = "as5011",
  293. },
  294. .probe = as5011_probe,
  295. .remove = __devexit_p(as5011_remove),
  296. .id_table = as5011_id,
  297. };
  298. static int __init as5011_init(void)
  299. {
  300. return i2c_add_driver(&as5011_driver);
  301. }
  302. module_init(as5011_init);
  303. static void __exit as5011_exit(void)
  304. {
  305. i2c_del_driver(&as5011_driver);
  306. }
  307. module_exit(as5011_exit);