iio: improve usage of gpiod API
[firefly-linux-kernel-4.4.55.git] / drivers / iio / accel / mma9551.c
1 /*
2  * Freescale MMA9551L Intelligent Motion-Sensing Platform driver
3  * Copyright (c) 2014, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/events.h>
25 #include <linux/pm_runtime.h>
26 #include "mma9551_core.h"
27
28 #define MMA9551_DRV_NAME                "mma9551"
29 #define MMA9551_IRQ_NAME                "mma9551_event"
30 #define MMA9551_GPIO_NAME               "mma9551_int"
31 #define MMA9551_GPIO_COUNT              4
32
33 /* Tilt application (inclination in IIO terms). */
34 #define MMA9551_TILT_XZ_ANG_REG         0x00
35 #define MMA9551_TILT_YZ_ANG_REG         0x01
36 #define MMA9551_TILT_XY_ANG_REG         0x02
37 #define MMA9551_TILT_ANGFLG             BIT(7)
38 #define MMA9551_TILT_QUAD_REG           0x03
39 #define MMA9551_TILT_XY_QUAD_SHIFT      0
40 #define MMA9551_TILT_YZ_QUAD_SHIFT      2
41 #define MMA9551_TILT_XZ_QUAD_SHIFT      4
42 #define MMA9551_TILT_CFG_REG            0x01
43 #define MMA9551_TILT_ANG_THRESH_MASK    GENMASK(3, 0)
44
45 #define MMA9551_DEFAULT_SAMPLE_RATE     122     /* Hz */
46
47 /* Tilt events are mapped to the first three GPIO pins. */
48 enum mma9551_tilt_axis {
49         mma9551_x = 0,
50         mma9551_y,
51         mma9551_z,
52 };
53
54 struct mma9551_data {
55         struct i2c_client *client;
56         struct mutex mutex;
57         int event_enabled[3];
58         int irqs[MMA9551_GPIO_COUNT];
59 };
60
61 static int mma9551_read_incli_chan(struct i2c_client *client,
62                                    const struct iio_chan_spec *chan,
63                                    int *val)
64 {
65         u8 quad_shift, angle, quadrant;
66         u16 reg_addr;
67         int ret;
68
69         switch (chan->channel2) {
70         case IIO_MOD_X:
71                 reg_addr = MMA9551_TILT_YZ_ANG_REG;
72                 quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
73                 break;
74         case IIO_MOD_Y:
75                 reg_addr = MMA9551_TILT_XZ_ANG_REG;
76                 quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
77                 break;
78         case IIO_MOD_Z:
79                 reg_addr = MMA9551_TILT_XY_ANG_REG;
80                 quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
81                 break;
82         default:
83                 return -EINVAL;
84         }
85
86         ret = mma9551_set_power_state(client, true);
87         if (ret < 0)
88                 return ret;
89
90         ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
91                                        reg_addr, &angle);
92         if (ret < 0)
93                 goto out_poweroff;
94
95         ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
96                                        MMA9551_TILT_QUAD_REG, &quadrant);
97         if (ret < 0)
98                 goto out_poweroff;
99
100         angle &= ~MMA9551_TILT_ANGFLG;
101         quadrant = (quadrant >> quad_shift) & 0x03;
102
103         if (quadrant == 1 || quadrant == 3)
104                 *val = 90 * (quadrant + 1) - angle;
105         else
106                 *val = angle + 90 * quadrant;
107
108         ret = IIO_VAL_INT;
109
110 out_poweroff:
111         mma9551_set_power_state(client, false);
112         return ret;
113 }
114
115 static int mma9551_read_raw(struct iio_dev *indio_dev,
116                             struct iio_chan_spec const *chan,
117                             int *val, int *val2, long mask)
118 {
119         struct mma9551_data *data = iio_priv(indio_dev);
120         int ret;
121
122         switch (mask) {
123         case IIO_CHAN_INFO_PROCESSED:
124                 switch (chan->type) {
125                 case IIO_INCLI:
126                         mutex_lock(&data->mutex);
127                         ret = mma9551_read_incli_chan(data->client, chan, val);
128                         mutex_unlock(&data->mutex);
129                         return ret;
130                 default:
131                         return -EINVAL;
132                 }
133         case IIO_CHAN_INFO_RAW:
134                 switch (chan->type) {
135                 case IIO_ACCEL:
136                         mutex_lock(&data->mutex);
137                         ret = mma9551_read_accel_chan(data->client,
138                                                       chan, val, val2);
139                         mutex_unlock(&data->mutex);
140                         return ret;
141                 default:
142                         return -EINVAL;
143                 }
144         case IIO_CHAN_INFO_SCALE:
145                 switch (chan->type) {
146                 case IIO_ACCEL:
147                         return mma9551_read_accel_scale(val, val2);
148                 default:
149                         return -EINVAL;
150                 }
151         default:
152                 return -EINVAL;
153         }
154 }
155
156 static int mma9551_read_event_config(struct iio_dev *indio_dev,
157                                      const struct iio_chan_spec *chan,
158                                      enum iio_event_type type,
159                                      enum iio_event_direction dir)
160 {
161         struct mma9551_data *data = iio_priv(indio_dev);
162
163         switch (chan->type) {
164         case IIO_INCLI:
165                 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
166                 return data->event_enabled[chan->channel2 - 1];
167         default:
168                 return -EINVAL;
169         }
170 }
171
172 static int mma9551_config_incli_event(struct iio_dev *indio_dev,
173                                       enum iio_modifier axis,
174                                       int state)
175 {
176         struct mma9551_data *data = iio_priv(indio_dev);
177         enum mma9551_tilt_axis mma_axis;
178         int ret;
179
180         /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
181         mma_axis = axis - 1;
182
183         if (data->event_enabled[mma_axis] == state)
184                 return 0;
185
186         if (state == 0) {
187                 ret = mma9551_gpio_config(data->client,
188                                           (enum mma9551_gpio_pin)mma_axis,
189                                           MMA9551_APPID_NONE, 0, 0);
190                 if (ret < 0)
191                         return ret;
192
193                 ret = mma9551_set_power_state(data->client, false);
194                 if (ret < 0)
195                         return ret;
196         } else {
197                 int bitnum;
198
199                 /* Bit 7 of each angle register holds the angle flag. */
200                 switch (axis) {
201                 case IIO_MOD_X:
202                         bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
203                         break;
204                 case IIO_MOD_Y:
205                         bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
206                         break;
207                 case IIO_MOD_Z:
208                         bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
209                         break;
210                 default:
211                         return -EINVAL;
212                 }
213
214
215                 ret = mma9551_set_power_state(data->client, true);
216                 if (ret < 0)
217                         return ret;
218
219                 ret = mma9551_gpio_config(data->client,
220                                           (enum mma9551_gpio_pin)mma_axis,
221                                           MMA9551_APPID_TILT, bitnum, 0);
222                 if (ret < 0) {
223                         mma9551_set_power_state(data->client, false);
224                         return ret;
225                 }
226         }
227
228         data->event_enabled[mma_axis] = state;
229
230         return ret;
231 }
232
233 static int mma9551_write_event_config(struct iio_dev *indio_dev,
234                                       const struct iio_chan_spec *chan,
235                                       enum iio_event_type type,
236                                       enum iio_event_direction dir,
237                                       int state)
238 {
239         struct mma9551_data *data = iio_priv(indio_dev);
240         int ret;
241
242         switch (chan->type) {
243         case IIO_INCLI:
244                 mutex_lock(&data->mutex);
245                 ret = mma9551_config_incli_event(indio_dev,
246                                                  chan->channel2, state);
247                 mutex_unlock(&data->mutex);
248                 return ret;
249         default:
250                 return -EINVAL;
251         }
252 }
253
254 static int mma9551_write_event_value(struct iio_dev *indio_dev,
255                                      const struct iio_chan_spec *chan,
256                                      enum iio_event_type type,
257                                      enum iio_event_direction dir,
258                                      enum iio_event_info info,
259                                      int val, int val2)
260 {
261         struct mma9551_data *data = iio_priv(indio_dev);
262         int ret;
263
264         switch (chan->type) {
265         case IIO_INCLI:
266                 if (val2 != 0 || val < 1 || val > 10)
267                         return -EINVAL;
268                 mutex_lock(&data->mutex);
269                 ret = mma9551_update_config_bits(data->client,
270                                                  MMA9551_APPID_TILT,
271                                                  MMA9551_TILT_CFG_REG,
272                                                  MMA9551_TILT_ANG_THRESH_MASK,
273                                                  val);
274                 mutex_unlock(&data->mutex);
275                 return ret;
276         default:
277                 return -EINVAL;
278         }
279 }
280
281 static int mma9551_read_event_value(struct iio_dev *indio_dev,
282                                     const struct iio_chan_spec *chan,
283                                     enum iio_event_type type,
284                                     enum iio_event_direction dir,
285                                     enum iio_event_info info,
286                                     int *val, int *val2)
287 {
288         struct mma9551_data *data = iio_priv(indio_dev);
289         int ret;
290         u8 tmp;
291
292         switch (chan->type) {
293         case IIO_INCLI:
294                 mutex_lock(&data->mutex);
295                 ret = mma9551_read_config_byte(data->client,
296                                                MMA9551_APPID_TILT,
297                                                MMA9551_TILT_CFG_REG, &tmp);
298                 mutex_unlock(&data->mutex);
299                 if (ret < 0)
300                         return ret;
301                 *val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
302                 *val2 = 0;
303                 return IIO_VAL_INT;
304         default:
305                 return -EINVAL;
306         }
307 }
308
309 static const struct iio_event_spec mma9551_incli_event = {
310         .type = IIO_EV_TYPE_ROC,
311         .dir = IIO_EV_DIR_RISING,
312         .mask_separate = BIT(IIO_EV_INFO_ENABLE),
313         .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
314 };
315
316 #define MMA9551_INCLI_CHANNEL(axis) {                           \
317         .type = IIO_INCLI,                                      \
318         .modified = 1,                                          \
319         .channel2 = axis,                                       \
320         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),     \
321         .event_spec = &mma9551_incli_event,                     \
322         .num_event_specs = 1,                                   \
323 }
324
325 static const struct iio_chan_spec mma9551_channels[] = {
326         MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
327         MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
328         MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
329
330         MMA9551_INCLI_CHANNEL(IIO_MOD_X),
331         MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
332         MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
333 };
334
335 static const struct iio_info mma9551_info = {
336         .driver_module = THIS_MODULE,
337         .read_raw = mma9551_read_raw,
338         .read_event_config = mma9551_read_event_config,
339         .write_event_config = mma9551_write_event_config,
340         .read_event_value = mma9551_read_event_value,
341         .write_event_value = mma9551_write_event_value,
342 };
343
344 static irqreturn_t mma9551_event_handler(int irq, void *private)
345 {
346         struct iio_dev *indio_dev = private;
347         struct mma9551_data *data = iio_priv(indio_dev);
348         int i, ret, mma_axis = -1;
349         u16 reg;
350         u8 val;
351
352         mutex_lock(&data->mutex);
353
354         for (i = 0; i < 3; i++)
355                 if (irq == data->irqs[i]) {
356                         mma_axis = i;
357                         break;
358                 }
359
360         if (mma_axis == -1) {
361                 /* IRQ was triggered on 4th line, which we don't use. */
362                 dev_warn(&data->client->dev,
363                          "irq triggered on unused line %d\n", data->irqs[3]);
364                 goto out;
365         }
366
367         switch (mma_axis) {
368         case mma9551_x:
369                 reg = MMA9551_TILT_YZ_ANG_REG;
370                 break;
371         case mma9551_y:
372                 reg = MMA9551_TILT_XZ_ANG_REG;
373                 break;
374         case mma9551_z:
375                 reg = MMA9551_TILT_XY_ANG_REG;
376                 break;
377         }
378
379         /*
380          * Read the angle even though we don't use it, otherwise we
381          * won't get any further interrupts.
382          */
383         ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
384                                        reg, &val);
385         if (ret < 0) {
386                 dev_err(&data->client->dev,
387                         "error %d reading tilt register in IRQ\n", ret);
388                 goto out;
389         }
390
391         iio_push_event(indio_dev,
392                        IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
393                                           IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
394                        iio_get_time_ns());
395
396 out:
397         mutex_unlock(&data->mutex);
398
399         return IRQ_HANDLED;
400 }
401
402 static int mma9551_init(struct mma9551_data *data)
403 {
404         int ret;
405
406         ret = mma9551_read_version(data->client);
407         if (ret)
408                 return ret;
409
410         return mma9551_set_device_state(data->client, true);
411 }
412
413 static int mma9551_gpio_probe(struct iio_dev *indio_dev)
414 {
415         struct gpio_desc *gpio;
416         int i, ret;
417         struct mma9551_data *data = iio_priv(indio_dev);
418         struct device *dev = &data->client->dev;
419
420         for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
421                 gpio = devm_gpiod_get_index(dev, MMA9551_GPIO_NAME, i,
422                                             GPIOD_IN);
423                 if (IS_ERR(gpio)) {
424                         dev_err(dev, "acpi gpio get index failed\n");
425                         return PTR_ERR(gpio);
426                 }
427
428                 data->irqs[i] = gpiod_to_irq(gpio);
429                 ret = devm_request_threaded_irq(dev, data->irqs[i],
430                                 NULL, mma9551_event_handler,
431                                 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
432                                 MMA9551_IRQ_NAME, indio_dev);
433                 if (ret < 0) {
434                         dev_err(dev, "request irq %d failed\n", data->irqs[i]);
435                         return ret;
436                 }
437
438                 dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
439                         desc_to_gpio(gpio), data->irqs[i]);
440         }
441
442         return 0;
443 }
444
445 static const char *mma9551_match_acpi_device(struct device *dev)
446 {
447         const struct acpi_device_id *id;
448
449         id = acpi_match_device(dev->driver->acpi_match_table, dev);
450         if (!id)
451                 return NULL;
452
453         return dev_name(dev);
454 }
455
456 static int mma9551_probe(struct i2c_client *client,
457                          const struct i2c_device_id *id)
458 {
459         struct mma9551_data *data;
460         struct iio_dev *indio_dev;
461         const char *name = NULL;
462         int ret;
463
464         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
465         if (!indio_dev)
466                 return -ENOMEM;
467
468         data = iio_priv(indio_dev);
469         i2c_set_clientdata(client, indio_dev);
470         data->client = client;
471
472         if (id)
473                 name = id->name;
474         else if (ACPI_HANDLE(&client->dev))
475                 name = mma9551_match_acpi_device(&client->dev);
476
477         ret = mma9551_init(data);
478         if (ret < 0)
479                 return ret;
480
481         mutex_init(&data->mutex);
482
483         indio_dev->dev.parent = &client->dev;
484         indio_dev->channels = mma9551_channels;
485         indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
486         indio_dev->name = name;
487         indio_dev->modes = INDIO_DIRECT_MODE;
488         indio_dev->info = &mma9551_info;
489
490         ret = mma9551_gpio_probe(indio_dev);
491         if (ret < 0)
492                 goto out_poweroff;
493
494         ret = iio_device_register(indio_dev);
495         if (ret < 0) {
496                 dev_err(&client->dev, "unable to register iio device\n");
497                 goto out_poweroff;
498         }
499
500         ret = pm_runtime_set_active(&client->dev);
501         if (ret < 0)
502                 goto out_iio_unregister;
503
504         pm_runtime_enable(&client->dev);
505         pm_runtime_set_autosuspend_delay(&client->dev,
506                                          MMA9551_AUTO_SUSPEND_DELAY_MS);
507         pm_runtime_use_autosuspend(&client->dev);
508
509         return 0;
510
511 out_iio_unregister:
512         iio_device_unregister(indio_dev);
513 out_poweroff:
514         mma9551_set_device_state(client, false);
515
516         return ret;
517 }
518
519 static int mma9551_remove(struct i2c_client *client)
520 {
521         struct iio_dev *indio_dev = i2c_get_clientdata(client);
522         struct mma9551_data *data = iio_priv(indio_dev);
523
524         pm_runtime_disable(&client->dev);
525         pm_runtime_set_suspended(&client->dev);
526         pm_runtime_put_noidle(&client->dev);
527
528         iio_device_unregister(indio_dev);
529         mutex_lock(&data->mutex);
530         mma9551_set_device_state(data->client, false);
531         mutex_unlock(&data->mutex);
532
533         return 0;
534 }
535
536 #ifdef CONFIG_PM
537 static int mma9551_runtime_suspend(struct device *dev)
538 {
539         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
540         struct mma9551_data *data = iio_priv(indio_dev);
541         int ret;
542
543         mutex_lock(&data->mutex);
544         ret = mma9551_set_device_state(data->client, false);
545         mutex_unlock(&data->mutex);
546         if (ret < 0) {
547                 dev_err(&data->client->dev, "powering off device failed\n");
548                 return -EAGAIN;
549         }
550
551         return 0;
552 }
553
554 static int mma9551_runtime_resume(struct device *dev)
555 {
556         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
557         struct mma9551_data *data = iio_priv(indio_dev);
558         int ret;
559
560         ret = mma9551_set_device_state(data->client, true);
561         if (ret < 0)
562                 return ret;
563
564         mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE);
565
566         return 0;
567 }
568 #endif
569
570 #ifdef CONFIG_PM_SLEEP
571 static int mma9551_suspend(struct device *dev)
572 {
573         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
574         struct mma9551_data *data = iio_priv(indio_dev);
575         int ret;
576
577         mutex_lock(&data->mutex);
578         ret = mma9551_set_device_state(data->client, false);
579         mutex_unlock(&data->mutex);
580
581         return ret;
582 }
583
584 static int mma9551_resume(struct device *dev)
585 {
586         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
587         struct mma9551_data *data = iio_priv(indio_dev);
588         int ret;
589
590         mutex_lock(&data->mutex);
591         ret = mma9551_set_device_state(data->client, true);
592         mutex_unlock(&data->mutex);
593
594         return ret;
595 }
596 #endif
597
598 static const struct dev_pm_ops mma9551_pm_ops = {
599         SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
600         SET_RUNTIME_PM_OPS(mma9551_runtime_suspend,
601                            mma9551_runtime_resume, NULL)
602 };
603
604 static const struct acpi_device_id mma9551_acpi_match[] = {
605         {"MMA9551", 0},
606         {},
607 };
608
609 MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
610
611 static const struct i2c_device_id mma9551_id[] = {
612         {"mma9551", 0},
613         {}
614 };
615
616 MODULE_DEVICE_TABLE(i2c, mma9551_id);
617
618 static struct i2c_driver mma9551_driver = {
619         .driver = {
620                    .name = MMA9551_DRV_NAME,
621                    .acpi_match_table = ACPI_PTR(mma9551_acpi_match),
622                    .pm = &mma9551_pm_ops,
623                    },
624         .probe = mma9551_probe,
625         .remove = mma9551_remove,
626         .id_table = mma9551_id,
627 };
628
629 module_i2c_driver(mma9551_driver);
630
631 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
632 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
633 MODULE_LICENSE("GPL v2");
634 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");