^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2010, 2011 Fabien Marteau <fabien.marteau@armadeus.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Sponsored by ARMadeus Systems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Driver for Austria Microsystems joysticks AS5011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * TODO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * - Power on the chip when open() and power down when close()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * - Manage power mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/input/as5011.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define DRIVER_DESC "Driver for Austria Microsystems AS5011 joystick"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MODULE_DEVICE_ALIAS "as5011"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) MODULE_AUTHOR("Fabien Marteau <fabien.marteau@armadeus.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define AS5011_CTRL1 0x76
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define AS5011_CTRL2 0x75
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define AS5011_XP 0x43
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define AS5011_XN 0x44
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define AS5011_YP 0x53
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define AS5011_YN 0x54
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define AS5011_X_REG 0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define AS5011_Y_REG 0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define AS5011_X_RES_INT 0x51
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define AS5011_Y_RES_INT 0x52
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* CTRL1 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define AS5011_CTRL1_LP_PULSED 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define AS5011_CTRL1_LP_ACTIVE 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define AS5011_CTRL1_LP_CONTINUE 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define AS5011_CTRL1_INT_WUP_EN 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define AS5011_CTRL1_INT_ACT_EN 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define AS5011_CTRL1_EXT_CLK_EN 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define AS5011_CTRL1_SOFT_RST 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define AS5011_CTRL1_DATA_VALID 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* CTRL2 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define AS5011_CTRL2_EXT_SAMPLE_EN 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define AS5011_CTRL2_RC_BIAS_ON 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define AS5011_CTRL2_INV_SPINNING 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define AS5011_MAX_AXIS 80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define AS5011_MIN_AXIS (-80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define AS5011_FUZZ 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define AS5011_FLAT 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct as5011_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct i2c_client *i2c_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int button_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int button_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int axis_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int as5011_i2c_write(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) uint8_t aregaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) uint8_t avalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) uint8_t data[2] = { aregaddr, avalue };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct i2c_msg msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .flags = I2C_M_IGNORE_NAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .len = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .buf = (uint8_t *)data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) error = i2c_transfer(client->adapter, &msg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return error < 0 ? error : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int as5011_i2c_read(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) uint8_t aregaddr, signed char *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) uint8_t data[2] = { aregaddr };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct i2c_msg msg_set[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .flags = I2C_M_REV_DIR_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .buf = (uint8_t *)data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .flags = I2C_M_RD | I2C_M_NOSTART,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .buf = (uint8_t *)data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) error = i2c_transfer(client->adapter, msg_set, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *value = data[0] & 0x80 ? -1 * (1 + ~data[0]) : data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static irqreturn_t as5011_button_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct as5011_device *as5011 = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int val = gpio_get_value_cansleep(as5011->button_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) input_report_key(as5011->input_dev, BTN_JOYSTICK, !val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) input_sync(as5011->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static irqreturn_t as5011_axis_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct as5011_device *as5011 = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) signed char x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) error = as5011_i2c_read(as5011->i2c_client, AS5011_X_RES_INT, &x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) error = as5011_i2c_read(as5011->i2c_client, AS5011_Y_RES_INT, &y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) input_report_abs(as5011->input_dev, ABS_X, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) input_report_abs(as5011->input_dev, ABS_Y, y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) input_sync(as5011->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int as5011_configure_chip(struct as5011_device *as5011,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct as5011_platform_data *plat_dat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct i2c_client *client = as5011->i2c_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) signed char value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* chip soft reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) error = as5011_i2c_write(client, AS5011_CTRL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) AS5011_CTRL1_SOFT_RST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) dev_err(&client->dev, "Soft reset failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mdelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) error = as5011_i2c_write(client, AS5011_CTRL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) AS5011_CTRL1_LP_PULSED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) AS5011_CTRL1_LP_ACTIVE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) AS5011_CTRL1_INT_ACT_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_err(&client->dev, "Power config failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) error = as5011_i2c_write(client, AS5011_CTRL2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) AS5011_CTRL2_INV_SPINNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev_err(&client->dev, "Can't invert spinning\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* write threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) error = as5011_i2c_write(client, AS5011_XP, plat_dat->xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_err(&client->dev, "Can't write threshold\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) error = as5011_i2c_write(client, AS5011_XN, plat_dat->xn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_err(&client->dev, "Can't write threshold\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) error = as5011_i2c_write(client, AS5011_YP, plat_dat->yp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev_err(&client->dev, "Can't write threshold\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) error = as5011_i2c_write(client, AS5011_YN, plat_dat->yn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) dev_err(&client->dev, "Can't write threshold\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* to free irq gpio in chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) error = as5011_i2c_read(client, AS5011_X_RES_INT, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev_err(&client->dev, "Can't read i2c X resolution value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int as5011_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) const struct as5011_platform_data *plat_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct as5011_device *as5011;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) plat_data = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!plat_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!plat_data->axis_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dev_err(&client->dev, "No axis IRQ?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) I2C_FUNC_NOSTART |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) I2C_FUNC_PROTOCOL_MANGLING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) "need i2c bus that supports protocol mangling\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!as5011 || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) "Can't allocate memory for device structure\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) as5011->i2c_client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) as5011->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) as5011->button_gpio = plat_data->button_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) as5011->axis_irq = plat_data->axis_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) input_dev->name = "Austria Microsystem as5011 joystick";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) input_dev->id.bustype = BUS_I2C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) input_dev->dev.parent = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) input_set_capability(input_dev, EV_KEY, BTN_JOYSTICK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) input_set_abs_params(input_dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) input_set_abs_params(as5011->input_dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) error = gpio_request(as5011->button_gpio, "AS5011 button");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev_err(&client->dev, "Failed to request button gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) irq = gpio_to_irq(as5011->button_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) "Failed to get irq number for button gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) error = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) goto err_free_button_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) as5011->button_irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) error = request_threaded_irq(as5011->button_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) NULL, as5011_button_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) IRQF_TRIGGER_RISING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) "as5011_button", as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) "Can't allocate button irq %d\n", as5011->button_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto err_free_button_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) error = as5011_configure_chip(as5011, plat_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto err_free_button_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) error = request_threaded_irq(as5011->axis_irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) as5011_axis_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) plat_data->axis_irqflags | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) "as5011_joystick", as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) "Can't allocate axis irq %d\n", plat_data->axis_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto err_free_button_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) error = input_register_device(as5011->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) dev_err(&client->dev, "Failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) goto err_free_axis_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) i2c_set_clientdata(client, as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) err_free_axis_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) free_irq(as5011->axis_irq, as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) err_free_button_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) free_irq(as5011->button_irq, as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err_free_button_gpio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) gpio_free(as5011->button_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) kfree(as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int as5011_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct as5011_device *as5011 = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) free_irq(as5011->axis_irq, as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) free_irq(as5011->button_irq, as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) gpio_free(as5011->button_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) input_unregister_device(as5011->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kfree(as5011);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static const struct i2c_device_id as5011_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) { MODULE_DEVICE_ALIAS, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) MODULE_DEVICE_TABLE(i2c, as5011_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static struct i2c_driver as5011_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .name = "as5011",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .probe = as5011_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .remove = as5011_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) .id_table = as5011_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) module_i2c_driver(as5011_driver);