diff options
author | Luwei Zhou <b45643@freescale.com> | 2013-12-19 21:23:39 -0500 |
---|---|---|
committer | Nitin Garg <nitin.garg@freescale.com> | 2014-04-16 09:47:30 -0400 |
commit | 74fc13798428613e7c996b3ab9bc26fbbff0e290 (patch) | |
tree | 7cd621ac8228c746c795d02fc45418d99a819698 | |
parent | b1ed2c2391ec6a2a324c764b475d2cc063c14a57 (diff) |
ENGR00281813 input: mma8450: evbug module will keep print message.
evbug will open the mma8450 on i.MX6SL_EVK and mma8450 will work in 2G mode by default.
That is the reason why mma8450 logs will be printed out. The main changes is below:
* Remove the open(), close() hook out of the drivers. The open() and close()
hook in input framwork is defined as void type. It isn't strictly safe in
logic when some error happends. So remove them out.
* Modify the mma8450 to standby mode by default. It will be more power saving
and there would be no log printing out after booting up.
* Provide the sys interface to modify the mma8450 work modes. Then the higher
layer can modify the the mma8450 work mode via the interface. It would be
much safer.There would be a sclaemode interface in the folder of
/sys/devices/soc0/soc.1/2100000.aips-bus/21a0000.i2c/i2c-0/0-001c/scalemode
User can use cat to read the current scalemode and echo to write. The mode
is defined as: MODE_STANDBY: 0 MODE_2G:1 MODE_4G:2 MODE_8G:3
* Add mutex to protect and some error handling.
Signed-off-by: Luwei Zhou <b45643@freescale.com>
-rw-r--r-- | drivers/input/misc/mma8450.c | 137 |
1 files changed, 123 insertions, 14 deletions
diff --git a/drivers/input/misc/mma8450.c b/drivers/input/misc/mma8450.c index 9d3868211fc2..2970df9c689d 100644 --- a/drivers/input/misc/mma8450.c +++ b/drivers/input/misc/mma8450.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include <linux/i2c.h> | 25 | #include <linux/i2c.h> |
26 | #include <linux/input-polldev.h> | 26 | #include <linux/input-polldev.h> |
27 | #include <linux/of_device.h> | 27 | #include <linux/of_device.h> |
28 | #include <linux/mutex.h> | ||
28 | 29 | ||
29 | #define MMA8450_DRV_NAME "mma8450" | 30 | #define MMA8450_DRV_NAME "mma8450" |
30 | 31 | ||
@@ -54,10 +55,19 @@ | |||
54 | #define MMA8450_ID 0xC6 | 55 | #define MMA8450_ID 0xC6 |
55 | #define MMA8450_WHO_AM_I 0x0F | 56 | #define MMA8450_WHO_AM_I 0x0F |
56 | 57 | ||
58 | enum { | ||
59 | MODE_STANDBY = 0, | ||
60 | MODE_2G, | ||
61 | MODE_4G, | ||
62 | MODE_8G, | ||
63 | }; | ||
64 | |||
57 | /* mma8450 status */ | 65 | /* mma8450 status */ |
58 | struct mma8450 { | 66 | struct mma8450 { |
59 | struct i2c_client *client; | 67 | struct i2c_client *client; |
60 | struct input_polled_dev *idev; | 68 | struct input_polled_dev *idev; |
69 | struct mutex mma8450_lock; | ||
70 | u8 mode; | ||
61 | }; | 71 | }; |
62 | 72 | ||
63 | static int mma8450_read(struct mma8450 *m, unsigned off) | 73 | static int mma8450_read(struct mma8450 *m, unsigned off) |
@@ -114,16 +124,19 @@ static void mma8450_poll(struct input_polled_dev *dev) | |||
114 | int ret; | 124 | int ret; |
115 | u8 buf[6]; | 125 | u8 buf[6]; |
116 | 126 | ||
117 | ret = mma8450_read(m, MMA8450_STATUS); | 127 | mutex_lock(&m->mma8450_lock); |
118 | if (ret < 0) | ||
119 | return; | ||
120 | 128 | ||
121 | if (!(ret & MMA8450_STATUS_ZXYDR)) | 129 | ret = mma8450_read(m, MMA8450_STATUS); |
130 | if (ret < 0 || !(ret & MMA8450_STATUS_ZXYDR)) { | ||
131 | mutex_unlock(&m->mma8450_lock); | ||
122 | return; | 132 | return; |
133 | } | ||
123 | 134 | ||
124 | ret = mma8450_read_block(m, MMA8450_OUT_X_LSB, buf, sizeof(buf)); | 135 | ret = mma8450_read_block(m, MMA8450_OUT_X_LSB, buf, sizeof(buf)); |
125 | if (ret < 0) | 136 | if (ret < 0) { |
137 | mutex_unlock(&m->mma8450_lock); | ||
126 | return; | 138 | return; |
139 | } | ||
127 | 140 | ||
128 | x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf); | 141 | x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf); |
129 | y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf); | 142 | y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf); |
@@ -133,10 +146,12 @@ static void mma8450_poll(struct input_polled_dev *dev) | |||
133 | input_report_abs(dev->input, ABS_Y, y); | 146 | input_report_abs(dev->input, ABS_Y, y); |
134 | input_report_abs(dev->input, ABS_Z, z); | 147 | input_report_abs(dev->input, ABS_Z, z); |
135 | input_sync(dev->input); | 148 | input_sync(dev->input); |
149 | |||
150 | mutex_unlock(&m->mma8450_lock); | ||
136 | } | 151 | } |
137 | 152 | ||
138 | /* Initialize the MMA8450 chip */ | 153 | /* Initialize the MMA8450 chip */ |
139 | static void mma8450_open(struct input_polled_dev *dev) | 154 | static s32 mma8450_open(struct input_polled_dev *dev) |
140 | { | 155 | { |
141 | struct mma8450 *m = dev->private; | 156 | struct mma8450 *m = dev->private; |
142 | int err; | 157 | int err; |
@@ -144,18 +159,20 @@ static void mma8450_open(struct input_polled_dev *dev) | |||
144 | /* enable all events from X/Y/Z, no FIFO */ | 159 | /* enable all events from X/Y/Z, no FIFO */ |
145 | err = mma8450_write(m, MMA8450_XYZ_DATA_CFG, 0x07); | 160 | err = mma8450_write(m, MMA8450_XYZ_DATA_CFG, 0x07); |
146 | if (err) | 161 | if (err) |
147 | return; | 162 | return err; |
148 | 163 | ||
149 | /* | 164 | /* |
150 | * Sleep mode poll rate - 50Hz | 165 | * Sleep mode poll rate - 50Hz |
151 | * System output data rate - 400Hz | 166 | * System output data rate - 400Hz |
152 | * Full scale selection - Active, +/- 2G | 167 | * Standby mode |
153 | */ | 168 | */ |
154 | err = mma8450_write(m, MMA8450_CTRL_REG1, 0x01); | 169 | err = mma8450_write(m, MMA8450_CTRL_REG1, MODE_STANDBY); |
155 | if (err < 0) | 170 | if (err) |
156 | return; | 171 | return err; |
157 | 172 | m->mode = MODE_STANDBY; | |
158 | msleep(MODE_CHANGE_DELAY_MS); | 173 | msleep(MODE_CHANGE_DELAY_MS); |
174 | |||
175 | return 0; | ||
159 | } | 176 | } |
160 | 177 | ||
161 | static void mma8450_close(struct input_polled_dev *dev) | 178 | static void mma8450_close(struct input_polled_dev *dev) |
@@ -166,6 +183,76 @@ static void mma8450_close(struct input_polled_dev *dev) | |||
166 | mma8450_write(m, MMA8450_CTRL_REG2, 0x01); | 183 | mma8450_write(m, MMA8450_CTRL_REG2, 0x01); |
167 | } | 184 | } |
168 | 185 | ||
186 | static ssize_t mma8450_scalemode_show(struct device *dev, | ||
187 | struct device_attribute *attr, | ||
188 | char *buf) | ||
189 | { | ||
190 | int mode = 0; | ||
191 | struct mma8450 *m; | ||
192 | struct i2c_client *client = to_i2c_client(dev); | ||
193 | |||
194 | m = i2c_get_clientdata(client); | ||
195 | |||
196 | mutex_lock(&m->mma8450_lock); | ||
197 | mode = (int)m->mode; | ||
198 | mutex_unlock(&m->mma8450_lock); | ||
199 | |||
200 | return sprintf(buf, "%d\n", mode); | ||
201 | } | ||
202 | |||
203 | static ssize_t mma8450_scalemode_store(struct device *dev, | ||
204 | struct device_attribute *attr, | ||
205 | const char *buf, size_t count) | ||
206 | { | ||
207 | unsigned long mode; | ||
208 | int ret; | ||
209 | struct mma8450 *m = NULL; | ||
210 | struct i2c_client *client = to_i2c_client(dev); | ||
211 | |||
212 | ret = strict_strtoul(buf, 10, &mode); | ||
213 | if (ret) { | ||
214 | dev_err(dev, "string transform error\n"); | ||
215 | return ret; | ||
216 | } | ||
217 | |||
218 | if (mode > MODE_8G) { | ||
219 | dev_warn(dev, "not supported mode %d\n", (int)mode); | ||
220 | return count; | ||
221 | } | ||
222 | |||
223 | m = i2c_get_clientdata(client); | ||
224 | |||
225 | mutex_lock(&m->mma8450_lock); | ||
226 | if (mode == m->mode) { | ||
227 | mutex_unlock(&m->mma8450_lock); | ||
228 | return count; | ||
229 | } | ||
230 | |||
231 | ret = mma8450_write(m, MMA8450_CTRL_REG1, mode); | ||
232 | if (ret < 0) { | ||
233 | mutex_unlock(&m->mma8450_lock); | ||
234 | return ret; | ||
235 | } | ||
236 | |||
237 | msleep(MODE_CHANGE_DELAY_MS); | ||
238 | m->mode = (u8)mode; | ||
239 | mutex_unlock(&m->mma8450_lock); | ||
240 | |||
241 | return count; | ||
242 | } | ||
243 | |||
244 | static DEVICE_ATTR(scalemode, S_IWUSR | S_IRUGO, | ||
245 | mma8450_scalemode_show, mma8450_scalemode_store); | ||
246 | |||
247 | static struct attribute *mma8450_attributes[] = { | ||
248 | &dev_attr_scalemode.attr, | ||
249 | NULL | ||
250 | }; | ||
251 | |||
252 | static const struct attribute_group mma8450_attr_group = { | ||
253 | .attrs = mma8450_attributes, | ||
254 | }; | ||
255 | |||
169 | /* | 256 | /* |
170 | * I2C init/probing/exit functions | 257 | * I2C init/probing/exit functions |
171 | */ | 258 | */ |
@@ -203,6 +290,7 @@ static int mma8450_probe(struct i2c_client *c, | |||
203 | 290 | ||
204 | m->client = c; | 291 | m->client = c; |
205 | m->idev = idev; | 292 | m->idev = idev; |
293 | i2c_set_clientdata(c, m); | ||
206 | 294 | ||
207 | idev->private = m; | 295 | idev->private = m; |
208 | idev->input->name = MMA8450_DRV_NAME; | 296 | idev->input->name = MMA8450_DRV_NAME; |
@@ -210,8 +298,6 @@ static int mma8450_probe(struct i2c_client *c, | |||
210 | idev->poll = mma8450_poll; | 298 | idev->poll = mma8450_poll; |
211 | idev->poll_interval = POLL_INTERVAL; | 299 | idev->poll_interval = POLL_INTERVAL; |
212 | idev->poll_interval_max = POLL_INTERVAL_MAX; | 300 | idev->poll_interval_max = POLL_INTERVAL_MAX; |
213 | idev->open = mma8450_open; | ||
214 | idev->close = mma8450_close; | ||
215 | 301 | ||
216 | __set_bit(EV_ABS, idev->input->evbit); | 302 | __set_bit(EV_ABS, idev->input->evbit); |
217 | input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32); | 303 | input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32); |
@@ -224,8 +310,28 @@ static int mma8450_probe(struct i2c_client *c, | |||
224 | goto err_free_mem; | 310 | goto err_free_mem; |
225 | } | 311 | } |
226 | 312 | ||
313 | mutex_init(&m->mma8450_lock); | ||
314 | |||
315 | err = mma8450_open(idev); | ||
316 | if (err) { | ||
317 | dev_err(&c->dev, "failed to initialize mma8450\n"); | ||
318 | goto err_unreg_dev; | ||
319 | } | ||
320 | |||
321 | err = sysfs_create_group(&c->dev.kobj, &mma8450_attr_group); | ||
322 | if (err) { | ||
323 | dev_err(&c->dev, "create device file failed!\n"); | ||
324 | err = -EINVAL; | ||
325 | goto err_close; | ||
326 | } | ||
327 | |||
227 | return 0; | 328 | return 0; |
228 | 329 | ||
330 | err_close: | ||
331 | mma8450_close(idev); | ||
332 | err_unreg_dev: | ||
333 | mutex_destroy(&m->mma8450_lock); | ||
334 | input_unregister_polled_device(idev); | ||
229 | err_free_mem: | 335 | err_free_mem: |
230 | input_free_polled_device(idev); | 336 | input_free_polled_device(idev); |
231 | kfree(m); | 337 | kfree(m); |
@@ -238,6 +344,9 @@ static int mma8450_remove(struct i2c_client *c) | |||
238 | struct mma8450 *m = i2c_get_clientdata(c); | 344 | struct mma8450 *m = i2c_get_clientdata(c); |
239 | struct input_polled_dev *idev = m->idev; | 345 | struct input_polled_dev *idev = m->idev; |
240 | 346 | ||
347 | sysfs_remove_group(&c->dev.kobj, &mma8450_attr_group); | ||
348 | mma8450_close(idev); | ||
349 | mutex_destroy(&m->mma8450_lock); | ||
241 | input_unregister_polled_device(idev); | 350 | input_unregister_polled_device(idev); |
242 | input_free_polled_device(idev); | 351 | input_free_polled_device(idev); |
243 | kfree(m); | 352 | kfree(m); |