as5011.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2010, 2011 Fabien Marteau <fabien.marteau@armadeus.com>
  4. * Sponsored by ARMadeus Systems
  5. *
  6. * Driver for Austria Microsystems joysticks AS5011
  7. *
  8. * TODO:
  9. * - Power on the chip when open() and power down when close()
  10. * - Manage power mode
  11. */
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/input.h>
  15. #include <linux/gpio.h>
  16. #include <linux/delay.h>
  17. #include <linux/input/as5011.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #define DRIVER_DESC "Driver for Austria Microsystems AS5011 joystick"
  21. #define MODULE_DEVICE_ALIAS "as5011"
  22. MODULE_AUTHOR("Fabien Marteau <fabien.marteau@armadeus.com>");
  23. MODULE_DESCRIPTION(DRIVER_DESC);
  24. MODULE_LICENSE("GPL");
  25. /* registers */
  26. #define AS5011_CTRL1 0x76
  27. #define AS5011_CTRL2 0x75
  28. #define AS5011_XP 0x43
  29. #define AS5011_XN 0x44
  30. #define AS5011_YP 0x53
  31. #define AS5011_YN 0x54
  32. #define AS5011_X_REG 0x41
  33. #define AS5011_Y_REG 0x42
  34. #define AS5011_X_RES_INT 0x51
  35. #define AS5011_Y_RES_INT 0x52
  36. /* CTRL1 bits */
  37. #define AS5011_CTRL1_LP_PULSED 0x80
  38. #define AS5011_CTRL1_LP_ACTIVE 0x40
  39. #define AS5011_CTRL1_LP_CONTINUE 0x20
  40. #define AS5011_CTRL1_INT_WUP_EN 0x10
  41. #define AS5011_CTRL1_INT_ACT_EN 0x08
  42. #define AS5011_CTRL1_EXT_CLK_EN 0x04
  43. #define AS5011_CTRL1_SOFT_RST 0x02
  44. #define AS5011_CTRL1_DATA_VALID 0x01
  45. /* CTRL2 bits */
  46. #define AS5011_CTRL2_EXT_SAMPLE_EN 0x08
  47. #define AS5011_CTRL2_RC_BIAS_ON 0x04
  48. #define AS5011_CTRL2_INV_SPINNING 0x02
  49. #define AS5011_MAX_AXIS 80
  50. #define AS5011_MIN_AXIS (-80)
  51. #define AS5011_FUZZ 8
  52. #define AS5011_FLAT 40
  53. struct as5011_device {
  54. struct input_dev *input_dev;
  55. struct i2c_client *i2c_client;
  56. unsigned int button_gpio;
  57. unsigned int button_irq;
  58. unsigned int axis_irq;
  59. };
  60. static int as5011_i2c_write(struct i2c_client *client,
  61. uint8_t aregaddr,
  62. uint8_t avalue)
  63. {
  64. uint8_t data[2] = { aregaddr, avalue };
  65. struct i2c_msg msg = {
  66. .addr = client->addr,
  67. .flags = I2C_M_IGNORE_NAK,
  68. .len = 2,
  69. .buf = (uint8_t *)data
  70. };
  71. int error;
  72. error = i2c_transfer(client->adapter, &msg, 1);
  73. return error < 0 ? error : 0;
  74. }
  75. static int as5011_i2c_read(struct i2c_client *client,
  76. uint8_t aregaddr, signed char *value)
  77. {
  78. uint8_t data[2] = { aregaddr };
  79. struct i2c_msg msg_set[2] = {
  80. {
  81. .addr = client->addr,
  82. .flags = I2C_M_REV_DIR_ADDR,
  83. .len = 1,
  84. .buf = (uint8_t *)data
  85. },
  86. {
  87. .addr = client->addr,
  88. .flags = I2C_M_RD | I2C_M_NOSTART,
  89. .len = 1,
  90. .buf = (uint8_t *)data
  91. }
  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 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 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 = dev_get_platdata(&client->dev);
  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_NOSTART |
  199. I2C_FUNC_PROTOCOL_MANGLING)) {
  200. dev_err(&client->dev,
  201. "need i2c bus that supports protocol mangling\n");
  202. return -ENODEV;
  203. }
  204. as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
  205. input_dev = input_allocate_device();
  206. if (!as5011 || !input_dev) {
  207. dev_err(&client->dev,
  208. "Can't allocate memory for device structure\n");
  209. error = -ENOMEM;
  210. goto err_free_mem;
  211. }
  212. as5011->i2c_client = client;
  213. as5011->input_dev = input_dev;
  214. as5011->button_gpio = plat_data->button_gpio;
  215. as5011->axis_irq = plat_data->axis_irq;
  216. input_dev->name = "Austria Microsystem as5011 joystick";
  217. input_dev->id.bustype = BUS_I2C;
  218. input_dev->dev.parent = &client->dev;
  219. input_set_capability(input_dev, EV_KEY, BTN_JOYSTICK);
  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. error = irq;
  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 |
  240. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  241. "as5011_button", as5011);
  242. if (error < 0) {
  243. dev_err(&client->dev,
  244. "Can't allocate button irq %d\n", as5011->button_irq);
  245. goto err_free_button_gpio;
  246. }
  247. error = as5011_configure_chip(as5011, plat_data);
  248. if (error)
  249. goto err_free_button_irq;
  250. error = request_threaded_irq(as5011->axis_irq, NULL,
  251. as5011_axis_interrupt,
  252. plat_data->axis_irqflags | IRQF_ONESHOT,
  253. "as5011_joystick", as5011);
  254. if (error) {
  255. dev_err(&client->dev,
  256. "Can't allocate axis irq %d\n", plat_data->axis_irq);
  257. goto err_free_button_irq;
  258. }
  259. error = input_register_device(as5011->input_dev);
  260. if (error) {
  261. dev_err(&client->dev, "Failed to register input device\n");
  262. goto err_free_axis_irq;
  263. }
  264. i2c_set_clientdata(client, as5011);
  265. return 0;
  266. err_free_axis_irq:
  267. free_irq(as5011->axis_irq, as5011);
  268. err_free_button_irq:
  269. free_irq(as5011->button_irq, as5011);
  270. err_free_button_gpio:
  271. gpio_free(as5011->button_gpio);
  272. err_free_mem:
  273. input_free_device(input_dev);
  274. kfree(as5011);
  275. return error;
  276. }
  277. static int as5011_remove(struct i2c_client *client)
  278. {
  279. struct as5011_device *as5011 = i2c_get_clientdata(client);
  280. free_irq(as5011->axis_irq, as5011);
  281. free_irq(as5011->button_irq, as5011);
  282. gpio_free(as5011->button_gpio);
  283. input_unregister_device(as5011->input_dev);
  284. kfree(as5011);
  285. return 0;
  286. }
  287. static const struct i2c_device_id as5011_id[] = {
  288. { MODULE_DEVICE_ALIAS, 0 },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(i2c, as5011_id);
  292. static struct i2c_driver as5011_driver = {
  293. .driver = {
  294. .name = "as5011",
  295. },
  296. .probe = as5011_probe,
  297. .remove = as5011_remove,
  298. .id_table = as5011_id,
  299. };
  300. module_i2c_driver(as5011_driver);