| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Driver for Freescale's 3-Axis Accelerometer MMA8450 |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 8 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | | - * (at your option) any later version. |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | | - * GNU General Public License for more details. |
|---|
| 15 | | - * |
|---|
| 16 | | - * You should have received a copy of the GNU General Public License |
|---|
| 17 | | - * along with this program; if not, write to the Free Software |
|---|
| 18 | | - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 19 | 6 | */ |
|---|
| 20 | 7 | |
|---|
| 21 | 8 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 23 | 10 | #include <linux/slab.h> |
|---|
| 24 | 11 | #include <linux/delay.h> |
|---|
| 25 | 12 | #include <linux/i2c.h> |
|---|
| 26 | | -#include <linux/input-polldev.h> |
|---|
| 13 | +#include <linux/input.h> |
|---|
| 27 | 14 | #include <linux/of_device.h> |
|---|
| 28 | 15 | |
|---|
| 29 | 16 | #define MMA8450_DRV_NAME "mma8450" |
|---|
| .. | .. |
|---|
| 52 | 39 | #define MMA8450_CTRL_REG1 0x38 |
|---|
| 53 | 40 | #define MMA8450_CTRL_REG2 0x39 |
|---|
| 54 | 41 | |
|---|
| 55 | | -/* mma8450 status */ |
|---|
| 56 | | -struct mma8450 { |
|---|
| 57 | | - struct i2c_client *client; |
|---|
| 58 | | - struct input_polled_dev *idev; |
|---|
| 59 | | -}; |
|---|
| 60 | | - |
|---|
| 61 | | -static int mma8450_read(struct mma8450 *m, unsigned off) |
|---|
| 42 | +static int mma8450_read(struct i2c_client *c, unsigned int off) |
|---|
| 62 | 43 | { |
|---|
| 63 | | - struct i2c_client *c = m->client; |
|---|
| 64 | 44 | int ret; |
|---|
| 65 | 45 | |
|---|
| 66 | 46 | ret = i2c_smbus_read_byte_data(c, off); |
|---|
| .. | .. |
|---|
| 72 | 52 | return ret; |
|---|
| 73 | 53 | } |
|---|
| 74 | 54 | |
|---|
| 75 | | -static int mma8450_write(struct mma8450 *m, unsigned off, u8 v) |
|---|
| 55 | +static int mma8450_write(struct i2c_client *c, unsigned int off, u8 v) |
|---|
| 76 | 56 | { |
|---|
| 77 | | - struct i2c_client *c = m->client; |
|---|
| 78 | 57 | int error; |
|---|
| 79 | 58 | |
|---|
| 80 | 59 | error = i2c_smbus_write_byte_data(c, off, v); |
|---|
| .. | .. |
|---|
| 88 | 67 | return 0; |
|---|
| 89 | 68 | } |
|---|
| 90 | 69 | |
|---|
| 91 | | -static int mma8450_read_block(struct mma8450 *m, unsigned off, |
|---|
| 70 | +static int mma8450_read_block(struct i2c_client *c, unsigned int off, |
|---|
| 92 | 71 | u8 *buf, size_t size) |
|---|
| 93 | 72 | { |
|---|
| 94 | | - struct i2c_client *c = m->client; |
|---|
| 95 | 73 | int err; |
|---|
| 96 | 74 | |
|---|
| 97 | 75 | err = i2c_smbus_read_i2c_block_data(c, off, size, buf); |
|---|
| .. | .. |
|---|
| 105 | 83 | return 0; |
|---|
| 106 | 84 | } |
|---|
| 107 | 85 | |
|---|
| 108 | | -static void mma8450_poll(struct input_polled_dev *dev) |
|---|
| 86 | +static void mma8450_poll(struct input_dev *input) |
|---|
| 109 | 87 | { |
|---|
| 110 | | - struct mma8450 *m = dev->private; |
|---|
| 88 | + struct i2c_client *c = input_get_drvdata(input); |
|---|
| 111 | 89 | int x, y, z; |
|---|
| 112 | 90 | int ret; |
|---|
| 113 | 91 | u8 buf[6]; |
|---|
| 114 | 92 | |
|---|
| 115 | | - ret = mma8450_read(m, MMA8450_STATUS); |
|---|
| 93 | + ret = mma8450_read(c, MMA8450_STATUS); |
|---|
| 116 | 94 | if (ret < 0) |
|---|
| 117 | 95 | return; |
|---|
| 118 | 96 | |
|---|
| 119 | 97 | if (!(ret & MMA8450_STATUS_ZXYDR)) |
|---|
| 120 | 98 | return; |
|---|
| 121 | 99 | |
|---|
| 122 | | - ret = mma8450_read_block(m, MMA8450_OUT_X_LSB, buf, sizeof(buf)); |
|---|
| 100 | + ret = mma8450_read_block(c, MMA8450_OUT_X_LSB, buf, sizeof(buf)); |
|---|
| 123 | 101 | if (ret < 0) |
|---|
| 124 | 102 | return; |
|---|
| 125 | 103 | |
|---|
| .. | .. |
|---|
| 127 | 105 | y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf); |
|---|
| 128 | 106 | z = ((int)(s8)buf[5] << 4) | (buf[4] & 0xf); |
|---|
| 129 | 107 | |
|---|
| 130 | | - input_report_abs(dev->input, ABS_X, x); |
|---|
| 131 | | - input_report_abs(dev->input, ABS_Y, y); |
|---|
| 132 | | - input_report_abs(dev->input, ABS_Z, z); |
|---|
| 133 | | - input_sync(dev->input); |
|---|
| 108 | + input_report_abs(input, ABS_X, x); |
|---|
| 109 | + input_report_abs(input, ABS_Y, y); |
|---|
| 110 | + input_report_abs(input, ABS_Z, z); |
|---|
| 111 | + input_sync(input); |
|---|
| 134 | 112 | } |
|---|
| 135 | 113 | |
|---|
| 136 | 114 | /* Initialize the MMA8450 chip */ |
|---|
| 137 | | -static void mma8450_open(struct input_polled_dev *dev) |
|---|
| 115 | +static int mma8450_open(struct input_dev *input) |
|---|
| 138 | 116 | { |
|---|
| 139 | | - struct mma8450 *m = dev->private; |
|---|
| 117 | + struct i2c_client *c = input_get_drvdata(input); |
|---|
| 140 | 118 | int err; |
|---|
| 141 | 119 | |
|---|
| 142 | 120 | /* enable all events from X/Y/Z, no FIFO */ |
|---|
| 143 | | - err = mma8450_write(m, MMA8450_XYZ_DATA_CFG, 0x07); |
|---|
| 121 | + err = mma8450_write(c, MMA8450_XYZ_DATA_CFG, 0x07); |
|---|
| 144 | 122 | if (err) |
|---|
| 145 | | - return; |
|---|
| 123 | + return err; |
|---|
| 146 | 124 | |
|---|
| 147 | 125 | /* |
|---|
| 148 | 126 | * Sleep mode poll rate - 50Hz |
|---|
| 149 | 127 | * System output data rate - 400Hz |
|---|
| 150 | 128 | * Full scale selection - Active, +/- 2G |
|---|
| 151 | 129 | */ |
|---|
| 152 | | - err = mma8450_write(m, MMA8450_CTRL_REG1, 0x01); |
|---|
| 153 | | - if (err < 0) |
|---|
| 154 | | - return; |
|---|
| 130 | + err = mma8450_write(c, MMA8450_CTRL_REG1, 0x01); |
|---|
| 131 | + if (err) |
|---|
| 132 | + return err; |
|---|
| 155 | 133 | |
|---|
| 156 | 134 | msleep(MODE_CHANGE_DELAY_MS); |
|---|
| 135 | + return 0; |
|---|
| 157 | 136 | } |
|---|
| 158 | 137 | |
|---|
| 159 | | -static void mma8450_close(struct input_polled_dev *dev) |
|---|
| 138 | +static void mma8450_close(struct input_dev *input) |
|---|
| 160 | 139 | { |
|---|
| 161 | | - struct mma8450 *m = dev->private; |
|---|
| 140 | + struct i2c_client *c = input_get_drvdata(input); |
|---|
| 162 | 141 | |
|---|
| 163 | | - mma8450_write(m, MMA8450_CTRL_REG1, 0x00); |
|---|
| 164 | | - mma8450_write(m, MMA8450_CTRL_REG2, 0x01); |
|---|
| 142 | + mma8450_write(c, MMA8450_CTRL_REG1, 0x00); |
|---|
| 143 | + mma8450_write(c, MMA8450_CTRL_REG2, 0x01); |
|---|
| 165 | 144 | } |
|---|
| 166 | 145 | |
|---|
| 167 | 146 | /* |
|---|
| .. | .. |
|---|
| 170 | 149 | static int mma8450_probe(struct i2c_client *c, |
|---|
| 171 | 150 | const struct i2c_device_id *id) |
|---|
| 172 | 151 | { |
|---|
| 173 | | - struct input_polled_dev *idev; |
|---|
| 174 | | - struct mma8450 *m; |
|---|
| 152 | + struct input_dev *input; |
|---|
| 175 | 153 | int err; |
|---|
| 176 | 154 | |
|---|
| 177 | | - m = devm_kzalloc(&c->dev, sizeof(*m), GFP_KERNEL); |
|---|
| 178 | | - if (!m) |
|---|
| 155 | + input = devm_input_allocate_device(&c->dev); |
|---|
| 156 | + if (!input) |
|---|
| 179 | 157 | return -ENOMEM; |
|---|
| 180 | 158 | |
|---|
| 181 | | - idev = devm_input_allocate_polled_device(&c->dev); |
|---|
| 182 | | - if (!idev) |
|---|
| 183 | | - return -ENOMEM; |
|---|
| 159 | + input_set_drvdata(input, c); |
|---|
| 184 | 160 | |
|---|
| 185 | | - m->client = c; |
|---|
| 186 | | - m->idev = idev; |
|---|
| 161 | + input->name = MMA8450_DRV_NAME; |
|---|
| 162 | + input->id.bustype = BUS_I2C; |
|---|
| 187 | 163 | |
|---|
| 188 | | - idev->private = m; |
|---|
| 189 | | - idev->input->name = MMA8450_DRV_NAME; |
|---|
| 190 | | - idev->input->id.bustype = BUS_I2C; |
|---|
| 191 | | - idev->poll = mma8450_poll; |
|---|
| 192 | | - idev->poll_interval = POLL_INTERVAL; |
|---|
| 193 | | - idev->poll_interval_max = POLL_INTERVAL_MAX; |
|---|
| 194 | | - idev->open = mma8450_open; |
|---|
| 195 | | - idev->close = mma8450_close; |
|---|
| 164 | + input->open = mma8450_open; |
|---|
| 165 | + input->close = mma8450_close; |
|---|
| 196 | 166 | |
|---|
| 197 | | - __set_bit(EV_ABS, idev->input->evbit); |
|---|
| 198 | | - input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32); |
|---|
| 199 | | - input_set_abs_params(idev->input, ABS_Y, -2048, 2047, 32, 32); |
|---|
| 200 | | - input_set_abs_params(idev->input, ABS_Z, -2048, 2047, 32, 32); |
|---|
| 167 | + input_set_abs_params(input, ABS_X, -2048, 2047, 32, 32); |
|---|
| 168 | + input_set_abs_params(input, ABS_Y, -2048, 2047, 32, 32); |
|---|
| 169 | + input_set_abs_params(input, ABS_Z, -2048, 2047, 32, 32); |
|---|
| 201 | 170 | |
|---|
| 202 | | - err = input_register_polled_device(idev); |
|---|
| 171 | + err = input_setup_polling(input, mma8450_poll); |
|---|
| 203 | 172 | if (err) { |
|---|
| 204 | | - dev_err(&c->dev, "failed to register polled input device\n"); |
|---|
| 173 | + dev_err(&c->dev, "failed to set up polling\n"); |
|---|
| 174 | + return err; |
|---|
| 175 | + } |
|---|
| 176 | + |
|---|
| 177 | + input_set_poll_interval(input, POLL_INTERVAL); |
|---|
| 178 | + input_set_max_poll_interval(input, POLL_INTERVAL_MAX); |
|---|
| 179 | + |
|---|
| 180 | + err = input_register_device(input); |
|---|
| 181 | + if (err) { |
|---|
| 182 | + dev_err(&c->dev, "failed to register input device\n"); |
|---|
| 205 | 183 | return err; |
|---|
| 206 | 184 | } |
|---|
| 207 | 185 | |
|---|