^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2011 Kionix, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Written by Chris Hudson <chudson@kionix.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/input/kxtj9.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define NAME "kxtj9"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define G_MAX 8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* OUTPUT REGISTERS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define XOUT_L 0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define WHO_AM_I 0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* CONTROL REGISTERS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define INT_REL 0x1A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define CTRL_REG1 0x1B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define INT_CTRL1 0x1E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define DATA_CTRL 0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* CONTROL REGISTER 1 BITS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define PC1_OFF 0x7F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define PC1_ON (1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Data ready funtion enable bit: set during probe if using irq mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DRDYE (1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* DATA CONTROL REGISTER BITS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define ODR12_5F 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define ODR25F 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define ODR50F 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define ODR100F 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define ODR200F 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define ODR400F 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define ODR800F 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* INTERRUPT CONTROL REGISTER 1 BITS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Set these during probe if using irq mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define KXTJ9_IEL (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define KXTJ9_IEA (1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define KXTJ9_IEN (1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* INPUT_ABS CONSTANTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define FUZZ 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define FLAT 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* RESUME STATE INDICES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define RES_DATA_CTRL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define RES_CTRL_REG1 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define RES_INT_CTRL1 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define RESUME_ENTRIES 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * The following table lists the maximum appropriate poll interval for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * available output data rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned int cutoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) } kxtj9_odr_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) { 3, ODR800F },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) { 5, ODR400F },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) { 10, ODR200F },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) { 20, ODR100F },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) { 40, ODR50F },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) { 80, ODR25F },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) { 0, ODR12_5F},
^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) struct kxtj9_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct kxtj9_platform_data pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int last_poll_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) u8 shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u8 ctrl_reg1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u8 data_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u8 int_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int kxtj9_i2c_read(struct kxtj9_data *tj9, u8 addr, u8 *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct i2c_msg msgs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .addr = tj9->client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .flags = tj9->client->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .buf = &addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .addr = tj9->client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .flags = tj9->client->flags | I2C_M_RD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .len = len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .buf = data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return i2c_transfer(tj9->client->adapter, msgs, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void kxtj9_report_acceleration_data(struct kxtj9_data *tj9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) s16 acc_data[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) s16 x, y, z;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) err = kxtj9_i2c_read(tj9, XOUT_L, (u8 *)acc_data, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_err(&tj9->client->dev, "accelerometer data read failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) x = le16_to_cpu(acc_data[tj9->pdata.axis_map_x]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) y = le16_to_cpu(acc_data[tj9->pdata.axis_map_y]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) z = le16_to_cpu(acc_data[tj9->pdata.axis_map_z]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) x >>= tj9->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) y >>= tj9->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) z >>= tj9->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) input_report_abs(tj9->input_dev, ABS_X, tj9->pdata.negate_x ? -x : x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) input_report_abs(tj9->input_dev, ABS_Y, tj9->pdata.negate_y ? -y : y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) input_report_abs(tj9->input_dev, ABS_Z, tj9->pdata.negate_z ? -z : z);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) input_sync(tj9->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static irqreturn_t kxtj9_isr(int irq, void *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct kxtj9_data *tj9 = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* data ready is the only possible interrupt type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) kxtj9_report_acceleration_data(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) err = i2c_smbus_read_byte_data(tj9->client, INT_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_err(&tj9->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) "error clearing interrupt status: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int kxtj9_update_g_range(struct kxtj9_data *tj9, u8 new_g_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) switch (new_g_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) case KXTJ9_G_2G:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) tj9->shift = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) case KXTJ9_G_4G:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) tj9->shift = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) case KXTJ9_G_8G:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) tj9->shift = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tj9->ctrl_reg1 &= 0xe7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) tj9->ctrl_reg1 |= new_g_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return 0;
^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) static int kxtj9_update_odr(struct kxtj9_data *tj9, unsigned int poll_interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Use the lowest ODR that can support the requested poll interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) for (i = 0; i < ARRAY_SIZE(kxtj9_odr_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) tj9->data_ctrl = kxtj9_odr_table[i].mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (poll_interval < kxtj9_odr_table[i].cutoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) err = i2c_smbus_write_byte_data(tj9->client, DATA_CTRL, tj9->data_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int kxtj9_device_power_on(struct kxtj9_data *tj9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (tj9->pdata.power_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return tj9->pdata.power_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void kxtj9_device_power_off(struct kxtj9_data *tj9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) tj9->ctrl_reg1 &= PC1_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dev_err(&tj9->client->dev, "soft power off failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (tj9->pdata.power_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) tj9->pdata.power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int kxtj9_enable(struct kxtj9_data *tj9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) err = kxtj9_device_power_on(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* ensure that PC1 is cleared before updating control registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* only write INT_CTRL_REG1 if in irq mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (tj9->client->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) err = i2c_smbus_write_byte_data(tj9->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) INT_CTRL1, tj9->int_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) err = kxtj9_update_g_range(tj9, tj9->pdata.g_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* turn on outputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) tj9->ctrl_reg1 |= PC1_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) err = kxtj9_update_odr(tj9, tj9->last_poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /* clear initial interrupt if in irq mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (tj9->client->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) err = i2c_smbus_read_byte_data(tj9->client, INT_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) dev_err(&tj9->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) "error clearing interrupt: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) kxtj9_device_power_off(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void kxtj9_disable(struct kxtj9_data *tj9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) kxtj9_device_power_off(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int kxtj9_input_open(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct kxtj9_data *tj9 = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return kxtj9_enable(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static void kxtj9_input_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct kxtj9_data *tj9 = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) kxtj9_disable(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * When IRQ mode is selected, we need to provide an interface to allow the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * to change the output data rate of the part. For consistency, we are using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * the set_poll method, which accepts a poll interval in milliseconds, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * calls update_odr() while passing this value as an argument. In IRQ mode, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * data outputs will not be read AT the requested poll interval, rather, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * lowest ODR that can support the requested interval. The client application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * will be responsible for retrieving data from the input node at the desired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * interval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /* Returns currently selected poll interval (in ms) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static ssize_t kxtj9_get_poll(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct kxtj9_data *tj9 = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return sprintf(buf, "%d\n", tj9->last_poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* Allow users to select a new poll interval (in ms) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static ssize_t kxtj9_set_poll(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct kxtj9_data *tj9 = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct input_dev *input_dev = tj9->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) unsigned int interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) error = kstrtouint(buf, 10, &interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /* Lock the device to prevent races with open/close (and itself) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) mutex_lock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) disable_irq(client->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * Set current interval to the greater of the minimum interval or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * the requested interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) tj9->last_poll_interval = max(interval, tj9->pdata.min_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) kxtj9_update_odr(tj9, tj9->last_poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) enable_irq(client->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) mutex_unlock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static DEVICE_ATTR(poll, S_IRUGO|S_IWUSR, kxtj9_get_poll, kxtj9_set_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static struct attribute *kxtj9_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) &dev_attr_poll.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static struct attribute_group kxtj9_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .attrs = kxtj9_attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static void kxtj9_poll(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct kxtj9_data *tj9 = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) unsigned int poll_interval = input_get_poll_interval(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) kxtj9_report_acceleration_data(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (poll_interval != tj9->last_poll_interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) kxtj9_update_odr(tj9, poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) tj9->last_poll_interval = poll_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^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) static void kxtj9_platform_exit(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct kxtj9_data *tj9 = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (tj9->pdata.exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) tj9->pdata.exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int kxtj9_verify(struct kxtj9_data *tj9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) retval = kxtj9_device_power_on(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) retval = i2c_smbus_read_byte_data(tj9->client, WHO_AM_I);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) dev_err(&tj9->client->dev, "read err int source\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) retval = (retval != 0x07 && retval != 0x08) ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) kxtj9_device_power_off(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static int kxtj9_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) const struct kxtj9_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct kxtj9_data *tj9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) dev_err(&client->dev, "client is not i2c capable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) dev_err(&client->dev, "platform data is NULL; exiting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) tj9 = devm_kzalloc(&client->dev, sizeof(*tj9), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (!tj9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) "failed to allocate memory for module data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) tj9->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) tj9->pdata = *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (pdata->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err = pdata->init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) err = devm_add_action_or_reset(&client->dev, kxtj9_platform_exit, tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) err = kxtj9_verify(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) dev_err(&client->dev, "device not recognized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) i2c_set_clientdata(client, tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) tj9->ctrl_reg1 = tj9->pdata.res_12bit | tj9->pdata.g_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) tj9->last_poll_interval = tj9->pdata.init_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) input_dev = devm_input_allocate_device(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (!input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) dev_err(&client->dev, "input device allocate failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) input_set_drvdata(input_dev, tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) tj9->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) input_dev->name = "kxtj9_accel";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) input_dev->id.bustype = BUS_I2C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) input_dev->open = kxtj9_input_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) input_dev->close = kxtj9_input_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) input_set_abs_params(input_dev, ABS_X, -G_MAX, G_MAX, FUZZ, FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) input_set_abs_params(input_dev, ABS_Y, -G_MAX, G_MAX, FUZZ, FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) input_set_abs_params(input_dev, ABS_Z, -G_MAX, G_MAX, FUZZ, FLAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (client->irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) err = input_setup_polling(input_dev, kxtj9_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) err = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) "unable to register input polled device %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) input_dev->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (client->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* If in irq mode, populate INT_CTRL_REG1 and enable DRDY. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) tj9->int_ctrl |= KXTJ9_IEN | KXTJ9_IEA | KXTJ9_IEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) tj9->ctrl_reg1 |= DRDYE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) err = devm_request_threaded_irq(&client->dev, client->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) NULL, kxtj9_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) IRQF_TRIGGER_RISING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) "kxtj9-irq", tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) dev_err(&client->dev, "request irq failed: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) err = devm_device_add_group(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) &kxtj9_attribute_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) dev_err(&client->dev, "sysfs create failed: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static int __maybe_unused kxtj9_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct kxtj9_data *tj9 = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct input_dev *input_dev = tj9->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) mutex_lock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (input_dev->users)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) kxtj9_disable(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) mutex_unlock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) static int __maybe_unused kxtj9_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct kxtj9_data *tj9 = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct input_dev *input_dev = tj9->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) mutex_lock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (input_dev->users)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) kxtj9_enable(tj9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) mutex_unlock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static SIMPLE_DEV_PM_OPS(kxtj9_pm_ops, kxtj9_suspend, kxtj9_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static const struct i2c_device_id kxtj9_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) { NAME, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) MODULE_DEVICE_TABLE(i2c, kxtj9_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static struct i2c_driver kxtj9_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) .name = NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) .pm = &kxtj9_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) .probe = kxtj9_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) .id_table = kxtj9_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) module_i2c_driver(kxtj9_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) MODULE_DESCRIPTION("KXTJ9 accelerometer driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) MODULE_LICENSE("GPL");