as5011.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. .addr = client->addr,
  80. .flags = I2C_M_IGNORE_NAK,
  81. .len = 2,
  82. .buf = (uint8_t *)data
  83. };
  84. int error;
  85. error = i2c_transfer(client->adapter, &msg, 1);
  86. return error < 0 ? error : 0;
  87. }
  88. static int as5011_i2c_read(struct i2c_client *client,
  89. uint8_t aregaddr, signed char *value)
  90. {
  91. uint8_t data[2] = { aregaddr };
  92. struct i2c_msg msg_set[2] = {
  93. {
  94. .addr = client->addr,
  95. .flags = I2C_M_REV_DIR_ADDR,
  96. .len = 1,
  97. .buf = (uint8_t *)data
  98. },
  99. {
  100. .addr = client->addr,
  101. .flags = I2C_M_RD | I2C_M_NOSTART,
  102. .len = 1,
  103. .buf = (uint8_t *)data
  104. }
  105. };
  106. int error;
  107. error = i2c_transfer(client->adapter, msg_set, 2);
  108. if (error < 0)
  109. return error;
  110. *value = data[0] & 0x80 ? -1 * (1 + ~data[0]) : data[0];
  111. return 0;
  112. }
  113. static irqreturn_t as5011_button_interrupt(int irq, void *dev_id)
  114. {
  115. struct as5011_device *as5011 = dev_id;
  116. int val = gpio_get_value_cansleep(as5011->button_gpio);
  117. input_report_key(as5011->input_dev, BTN_JOYSTICK, !val);
  118. input_sync(as5011->input_dev);
  119. return IRQ_HANDLED;
  120. }
  121. static irqreturn_t as5011_axis_interrupt(int irq, void *dev_id)
  122. {
  123. struct as5011_device *as5011 = dev_id;
  124. int error;
  125. signed char x, y;
  126. error = as5011_i2c_read(as5011->i2c_client, AS5011_X_RES_INT, &x);
  127. if (error < 0)
  128. goto out;
  129. error = as5011_i2c_read(as5011->i2c_client, AS5011_Y_RES_INT, &y);
  130. if (error < 0)
  131. goto out;
  132. input_report_abs(as5011->input_dev, ABS_X, x);
  133. input_report_abs(as5011->input_dev, ABS_Y, y);
  134. input_sync(as5011->input_dev);
  135. out:
  136. return IRQ_HANDLED;
  137. }
  138. static int as5011_configure_chip(struct as5011_device *as5011,
  139. const struct as5011_platform_data *plat_dat)
  140. {
  141. struct i2c_client *client = as5011->i2c_client;
  142. int error;
  143. signed char value;
  144. /* chip soft reset */
  145. error = as5011_i2c_write(client, AS5011_CTRL1,
  146. AS5011_CTRL1_SOFT_RST);
  147. if (error < 0) {
  148. dev_err(&client->dev, "Soft reset failed\n");
  149. return error;
  150. }
  151. mdelay(10);
  152. error = as5011_i2c_write(client, AS5011_CTRL1,
  153. AS5011_CTRL1_LP_PULSED |
  154. AS5011_CTRL1_LP_ACTIVE |
  155. AS5011_CTRL1_INT_ACT_EN);
  156. if (error < 0) {
  157. dev_err(&client->dev, "Power config failed\n");
  158. return error;
  159. }
  160. error = as5011_i2c_write(client, AS5011_CTRL2,
  161. AS5011_CTRL2_INV_SPINNING);
  162. if (error < 0) {
  163. dev_err(&client->dev, "Can't invert spinning\n");
  164. return error;
  165. }
  166. /* write threshold */
  167. error = as5011_i2c_write(client, AS5011_XP, plat_dat->xp);
  168. if (error < 0) {
  169. dev_err(&client->dev, "Can't write threshold\n");
  170. return error;
  171. }
  172. error = as5011_i2c_write(client, AS5011_XN, plat_dat->xn);
  173. if (error < 0) {
  174. dev_err(&client->dev, "Can't write threshold\n");
  175. return error;
  176. }
  177. error = as5011_i2c_write(client, AS5011_YP, plat_dat->yp);
  178. if (error < 0) {
  179. dev_err(&client->dev, "Can't write threshold\n");
  180. return error;
  181. }
  182. error = as5011_i2c_write(client, AS5011_YN, plat_dat->yn);
  183. if (error < 0) {
  184. dev_err(&client->dev, "Can't write threshold\n");
  185. return error;
  186. }
  187. /* to free irq gpio in chip */
  188. error = as5011_i2c_read(client, AS5011_X_RES_INT, &value);
  189. if (error < 0) {
  190. dev_err(&client->dev, "Can't read i2c X resolution value\n");
  191. return error;
  192. }
  193. return 0;
  194. }
  195. static int as5011_probe(struct i2c_client *client,
  196. const struct i2c_device_id *id)
  197. {
  198. const struct as5011_platform_data *plat_data;
  199. struct as5011_device *as5011;
  200. struct input_dev *input_dev;
  201. int irq;
  202. int error;
  203. plat_data = dev_get_platdata(&client->dev);
  204. if (!plat_data)
  205. return -EINVAL;
  206. if (!plat_data->axis_irq) {
  207. dev_err(&client->dev, "No axis IRQ?\n");
  208. return -EINVAL;
  209. }
  210. if (!i2c_check_functionality(client->adapter,
  211. I2C_FUNC_NOSTART |
  212. I2C_FUNC_PROTOCOL_MANGLING)) {
  213. dev_err(&client->dev,
  214. "need i2c bus that supports protocol mangling\n");
  215. return -ENODEV;
  216. }
  217. as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
  218. input_dev = input_allocate_device();
  219. if (!as5011 || !input_dev) {
  220. dev_err(&client->dev,
  221. "Can't allocate memory for device structure\n");
  222. error = -ENOMEM;
  223. goto err_free_mem;
  224. }
  225. as5011->i2c_client = client;
  226. as5011->input_dev = input_dev;
  227. as5011->button_gpio = plat_data->button_gpio;
  228. as5011->axis_irq = plat_data->axis_irq;
  229. input_dev->name = "Austria Microsystem as5011 joystick";
  230. input_dev->id.bustype = BUS_I2C;
  231. input_dev->dev.parent = &client->dev;
  232. __set_bit(EV_KEY, input_dev->evbit);
  233. __set_bit(EV_ABS, input_dev->evbit);
  234. __set_bit(BTN_JOYSTICK, input_dev->keybit);
  235. input_set_abs_params(input_dev, ABS_X,
  236. AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
  237. input_set_abs_params(as5011->input_dev, ABS_Y,
  238. AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
  239. error = gpio_request(as5011->button_gpio, "AS5011 button");
  240. if (error < 0) {
  241. dev_err(&client->dev, "Failed to request button gpio\n");
  242. goto err_free_mem;
  243. }
  244. irq = gpio_to_irq(as5011->button_gpio);
  245. if (irq < 0) {
  246. dev_err(&client->dev,
  247. "Failed to get irq number for button gpio\n");
  248. error = irq;
  249. goto err_free_button_gpio;
  250. }
  251. as5011->button_irq = irq;
  252. error = request_threaded_irq(as5011->button_irq,
  253. NULL, as5011_button_interrupt,
  254. IRQF_TRIGGER_RISING |
  255. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  256. "as5011_button", as5011);
  257. if (error < 0) {
  258. dev_err(&client->dev,
  259. "Can't allocate button irq %d\n", as5011->button_irq);
  260. goto err_free_button_gpio;
  261. }
  262. error = as5011_configure_chip(as5011, plat_data);
  263. if (error)
  264. goto err_free_button_irq;
  265. error = request_threaded_irq(as5011->axis_irq, NULL,
  266. as5011_axis_interrupt,
  267. plat_data->axis_irqflags | IRQF_ONESHOT,
  268. "as5011_joystick", as5011);
  269. if (error) {
  270. dev_err(&client->dev,
  271. "Can't allocate axis irq %d\n", plat_data->axis_irq);
  272. goto err_free_button_irq;
  273. }
  274. error = input_register_device(as5011->input_dev);
  275. if (error) {
  276. dev_err(&client->dev, "Failed to register input device\n");
  277. goto err_free_axis_irq;
  278. }
  279. i2c_set_clientdata(client, as5011);
  280. return 0;
  281. err_free_axis_irq:
  282. free_irq(as5011->axis_irq, as5011);
  283. err_free_button_irq:
  284. free_irq(as5011->button_irq, as5011);
  285. err_free_button_gpio:
  286. gpio_free(as5011->button_gpio);
  287. err_free_mem:
  288. input_free_device(input_dev);
  289. kfree(as5011);
  290. return error;
  291. }
  292. static int as5011_remove(struct i2c_client *client)
  293. {
  294. struct as5011_device *as5011 = i2c_get_clientdata(client);
  295. free_irq(as5011->axis_irq, as5011);
  296. free_irq(as5011->button_irq, as5011);
  297. gpio_free(as5011->button_gpio);
  298. input_unregister_device(as5011->input_dev);
  299. kfree(as5011);
  300. return 0;
  301. }
  302. static const struct i2c_device_id as5011_id[] = {
  303. { MODULE_DEVICE_ALIAS, 0 },
  304. { }
  305. };
  306. MODULE_DEVICE_TABLE(i2c, as5011_id);
  307. static struct i2c_driver as5011_driver = {
  308. .driver = {
  309. .name = "as5011",
  310. },
  311. .probe = as5011_probe,
  312. .remove = as5011_remove,
  313. .id_table = as5011_id,
  314. };
  315. module_i2c_driver(as5011_driver);