Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings hostmode easy now h-e-n
Summary Forums Tracker Lists Tasks Docs News SCM Files Wiki
1 /*
2  * drivers/i2c/chips/lis302dl.c
3  * Driver for STMicroelectronics LIS302DL acceleration sensor
4  *
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * Written by Henrik Saari <henrik.saari@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/io.h>
29 #include <linux/platform_device.h>
30 #include <linux/gpio.h>
31 #include <linux/lis302dl.h>
32 #include <mach/board.h>
34 #define DRIVER_NAME  "lis302dl"
36 #define LIS302_WHOAMI                   0x0f
37 #define LIS302_CTRL_1                   0x20
38 #       define LIS302_CTRL1_DR          (1 << 7)
39 #       define LIS302_CTRL1_PD          (1 << 6)
40 #       define LIS302_CTRL1_FS          (1 << 5)
41 #       define LIS302_CTRL1_STP         (1 << 4)
42 #       define LIS302_CTRL1_STM         (1 << 3)
43 #       define LIS302_CTRL1_Z           (1 << 2)
44 #       define LIS302_CTRL1_Y           (1 << 1)
45 #       define LIS302_CTRL1_X           (1 << 0)
46 #define LIS302_CTRL_2                   0x21
47 #       define LIS302_CTRL2_BOOT        (1 << 6)
48 #define LIS302_CTRL_3                   0x22
49 #       define  LIS302_CTRL3_GND        0x00
50 #       define  LIS302_CTRL3_FF_WU_1    0x01
51 #       define  LIS302_CTRL3_FF_WU_2    0x02
52 #       define  LIS302_CTRL3_FF_WU_12   0x03
53 #       define  LIS302_CTRL3_DATA_RDY   0x04
54 #       define  LIS302_CTRL3_CLICK      0x07
55 #define LIS302_HP_FILTER_RESET          0x23
56 #define LIS302_STATUS_REG               0x27
57 #define LIS302_X                        0x29
58 #define LIS302_Y                        0x2b
59 #define LIS302_Z                        0x2d
60 #define LIS302_FF_WU_CFG_1              0x30
62 /* configurable interrupt events */
63 #define LIS302_X_LOW                    (1 << 0)
64 #define LIS302_X_HIGH                   (1 << 1)
65 #define LIS302_Y_LOW                    (1 << 2)
66 #define LIS302_Y_HIGH                   (1 << 3)
67 #define LIS302_Z_LOW                    (1 << 4)
68 #define LIS302_Z_HIGH                   (1 << 5)
69 #define LIS302_LIR                      (1 << 6)
70 #define LIS302_AOI                      (1 << 7)
72 #define LIS302_FF_WU_SRC_1              0x31
73 #define LIS302_FF_THS_1                 0x32
74 #define LIS302_FF_WU_DURATION_1         0x33
75 #define LIS302_FF_WU_CFG_2              0x34
76 #define LIS302_FF_WU_SRC_2              0x34
77 #define LIS302_FF_THS_2                 0x35
78 #define LIS302_FF_WU_DURATION_2         0x37
80 /* Default values */
81 #define LIS302_THS              810     /* mg    */
82 #define LIS302_DURATION         500     /* ms    */
83 #define LIS302_400HZ            1       /* sample rate 400Hz */
84 #define LIS302_100HZ            0       /* sample rate 100Hz */
85 #define LIS302_FS               0       /* full scale 0 / 1 */
86 #define LIS302_SAMPLES          1
87 #define LIS302_SMALL_UNIT       18      /* Typical value 18 mg/digit */
88 #define LIS302_BIG_UNIT         72      /* Typical value 72 mg/digit */
89 #define LIS302_TURN_ON_TIME     3000    /* Turn on time 3000ms / data rate */
91 #define LIS302_POWEROFF_DELAY   (5 * HZ)
93 /* A lis302dl chip will contain this value in LIS302_WHOAMI register */
94 #define LIS302_WHOAMI_VALUE     0x3b
95 #define LIS302_IRQ_FLAGS (IRQF_TRIGGER_RISING | IRQF_SAMPLE_RANDOM)
97 struct lis302dl_chip {
98         struct mutex            lock;
99         struct i2c_client       *client;
100         struct work_struct      work1, work2;
101         struct delayed_work     poweroff_work;
102         int                     irq1, irq2;
103         uint8_t                 power;
104         int                     threshold;
105         int                     duration;
106         uint8_t                 sample_rate;
107         uint8_t                 fs;
108         unsigned int            samples;
109 };
111 static inline s32 lis302dl_write(struct i2c_client *c, int reg, u8 value)
113         return i2c_smbus_write_byte_data(c, reg, value);
116 static inline s32 lis302dl_read(struct i2c_client *c, int reg)
118         return i2c_smbus_read_byte_data(c, reg);
121 /*
122  * Detect LIS302DL chip. Return value is zero if
123  * chip detected, otherwise a negative error code.
124  */
125 static int lis302dl_detect(struct i2c_client *c)
127         int r;
129         r = lis302dl_read(c, LIS302_WHOAMI);
130         if (r < 0)
131                 return r;
133         if (r != LIS302_WHOAMI_VALUE)
134                 return -ENODEV;
136         return 0;
139 static inline u8 intmode(int pin, u8 mode)
141         if (pin == 1)
142                 return mode;
143         if (pin == 2)
144                 return (mode << 3);
146         return 0;
149 static int lis302dl_configure(struct i2c_client *c)
152         struct lis302dl_chip *lis = i2c_get_clientdata(c);
153         int ts = 0, ret;
154         u8 duration, r = 0;
156         /* REG 1*/
157         /* Controls power, scale, data rate, and enabled axis */
158         r |= lis->sample_rate ? LIS302_CTRL1_DR : 0;
159         r |= lis->fs ? LIS302_CTRL1_FS : 0;
160         r |= LIS302_CTRL1_PD | LIS302_CTRL1_X | LIS302_CTRL1_Y | LIS302_CTRL1_Z;
161         ret = lis302dl_write(c, LIS302_CTRL_1, r);
162         if (ret < 0)
163                 goto out;
165         /* REG 2
166          * Boot is used to refresh internal registers
167          * Control High Pass filter selection. not used
168          */
169         ret = lis302dl_write(c, LIS302_CTRL_2, LIS302_CTRL2_BOOT);
170         if (ret < 0)
171                 goto out;
173         /* REG 3
174          * Interrupt CTRL register. One interrupt pin is used for
175          * inertial wakeup
176         */
177         r = intmode(1, LIS302_CTRL3_FF_WU_1) | intmode(2, LIS302_CTRL3_GND);
178         ret = lis302dl_write(c, LIS302_CTRL_3, r);
179         if (ret < 0)
180                 goto out;
182         /* Configure interrupt pin thresholds */
183         ts = lis->threshold / (lis->fs ? LIS302_BIG_UNIT : LIS302_SMALL_UNIT);
184         ts &= 0x7f;
185         duration = lis->duration / (lis->sample_rate ? 40 : 10);
187         ret = lis302dl_write(c, LIS302_FF_THS_1, ts);
188         if (ret < 0)
189                 goto out;
190         ret = lis302dl_write(c, LIS302_FF_WU_DURATION_1, duration);
191         if (ret < 0)
192                 goto out;
193         /* Enable interrupt wakeup on x and y axis */
194         ret = lis302dl_write(c, LIS302_FF_WU_CFG_1,
195                              (LIS302_X_HIGH | LIS302_Y_HIGH));
196         if (ret < 0)
197                 goto out;
198  out:
199         return ret;
202 static inline void lis302dl_print_event(struct device *dev, u8 event)
204         if (event & 0x01)
205                 dev_dbg(dev, "X Low event\n");
206         if (event & 0x02)
207                 dev_dbg(dev, "X High event\n");
208         if (event & 0x04)
209                 dev_dbg(dev, "Y Low event\n");
210         if (event & 0x08)
211                 dev_dbg(dev, "Y High event\n");
212         if (event & 0x10)
213                 dev_dbg(dev, "Z Low event\n");
214         if (event & 0x20)
215                 dev_dbg(dev, "Z High event\n");
218 /* Interrupt handler bottom halves. */
219 static void lis302dl_work1(struct work_struct  *work)
221         struct lis302dl_chip *chip =
222                 container_of(work, struct lis302dl_chip, work1);
223         u8 reg;
225         mutex_lock(&chip->lock);
226         /* ack the interrupt */
227         reg = lis302dl_read(chip->client, LIS302_FF_WU_SRC_1);
228         mutex_unlock(&chip->lock);
229         sysfs_notify(&chip->client->dev.kobj, NULL, "coord");
230         lis302dl_print_event(&chip->client->dev, reg);
233 static void lis302dl_work2(struct work_struct *work)
235         struct lis302dl_chip *chip =
236                 container_of(work, struct lis302dl_chip, work2);
237         u8 reg;
239         mutex_lock(&chip->lock);
240         /* ack the interrupt */
241         reg = lis302dl_read(chip->client, LIS302_FF_WU_SRC_2);
242         mutex_unlock(&chip->lock);
243         lis302dl_print_event(&chip->client->dev, reg);
246 /*
247  * We cannot use I2C in interrupt context, so we just schedule work.
248  */
249 static irqreturn_t lis302dl_irq1(int irq, void *_chip)
251         struct lis302dl_chip *chip = _chip;
252         schedule_work(&chip->work1);
254         return IRQ_HANDLED;
257 static irqreturn_t lis302dl_irq2(int irq, void *_chip)
259         return IRQ_HANDLED;
262 /* duration depends on chips data rate */
263 static void set_duration(struct i2c_client *c, int dr, int msec)
265         u8 duration;
266         if (dr)
267                 /* 400 Hz data rate max duration is 637.5 ms */
268                 if (msec > 637)
269                         duration = 0xff;
270                 else
271                         duration = (msec / 10) * 4;
272         else
273                 duration = msec / 10;
274         lis302dl_write(c, LIS302_FF_WU_DURATION_1, duration);
277 static void set_ths(struct i2c_client *c, int full_scale, int ths)
279         u8 threshold;
281         if (full_scale)
282                 threshold = ths / LIS302_BIG_UNIT;
283         else
284                 /* max threshold is 2286 mg when normal scale is used*/
285                 if (ths > (127 * LIS302_SMALL_UNIT))
286                         threshold = 0x7f;
287                 else
288                         threshold = ths / LIS302_SMALL_UNIT;
290         threshold &= 0x7f;
291         lis302dl_write(c, LIS302_FF_THS_1, threshold);
294 static int lis302dl_power(struct lis302dl_chip *chip, int on)
296         u8 reg, regwant;
297         int result, delay;
299         reg = lis302dl_read(chip->client, LIS302_CTRL_1);
300         if (on)
301                 regwant = reg | LIS302_CTRL1_PD;
302         else
303                 regwant = reg & ~LIS302_CTRL1_PD;
305         /* Avoid unnecessary writes */
306         if (reg == regwant)
307                 return 0;
309         result = lis302dl_write(chip->client, LIS302_CTRL_1, regwant);
311         /* turn on time delay depends on data rate */
312         if (on) {
313                 delay = (chip->sample_rate ? (LIS302_TURN_ON_TIME / 400) :
314                          (LIS302_TURN_ON_TIME / 100)) + 1;
315                 msleep(delay);
316         }
317         if (!result)
318                 chip->power = !!on;
320         return !!result;
323 static void lis302dl_poweroff_work(struct work_struct *work)
325         struct lis302dl_chip *chip =
326                 container_of(work, struct lis302dl_chip, poweroff_work.work);
327         mutex_lock(&chip->lock);
328         lis302dl_power(chip, 0);
329         mutex_unlock(&chip->lock);
332 static int lis302dl_selftest(struct lis302dl_chip *chip)
334         u8 reg;
335         s8 x, y, z;
336         s8 powerbit;
338         reg = lis302dl_read(chip->client, LIS302_CTRL_1);
339         powerbit = reg & LIS302_CTRL1_PD;
340         reg |= LIS302_CTRL1_PD;
341         lis302dl_write(chip->client, LIS302_CTRL_1, (reg | LIS302_CTRL1_STP));
342         msleep(30);
343         x = (s8)lis302dl_read(chip->client, LIS302_X);
344         y = (s8)lis302dl_read(chip->client, LIS302_Y);
345         z = (s8)lis302dl_read(chip->client, LIS302_Z);
346         /* back to normal settings */
347         lis302dl_write(chip->client, LIS302_CTRL_1, reg);
348         msleep(30);
349         x -= (s8)lis302dl_read(chip->client, LIS302_X);
350         y -= (s8)lis302dl_read(chip->client, LIS302_Y);
351         z -= (s8)lis302dl_read(chip->client, LIS302_Z);
353         /* Return to passive state if we were in it. */
354         if (!powerbit)
355                 lis302dl_write(chip->client,
356                                LIS302_CTRL_1,
357                                reg & ~LIS302_CTRL1_PD);
359         /* Now check that delta is within specified range for each axis */
360         if (x < -32 || x > -3)
361                 return -1;
362         if (y < 3 || y > 32)
363                 return -1;
364         if (z < 3 || z > 32)
365                 return -1;
367         /* test passed */
368         return 0;
371 /*******************************************************************************
372  * SYSFS                                                                       *
373  ******************************************************************************/
375 static ssize_t lis302dl_show_power(struct device *dev,
376                                    struct device_attribute *attr, char *buf)
378         int val;
379         int ret;
380         struct lis302dl_chip *chip = dev_get_drvdata(dev);
382         mutex_lock(&chip->lock);
383         val = lis302dl_read(chip->client, LIS302_CTRL_1);
384         if (val >= 0)
385                 if (val & LIS302_CTRL1_PD)
386                         ret = snprintf(buf, PAGE_SIZE, "on\n");
387                 else
388                         ret = snprintf(buf, PAGE_SIZE, "off\n");
389         else
390                 ret = val;
391         mutex_unlock(&chip->lock);
392         return ret;
395 static ssize_t lis302dl_set_power(struct device *dev,
396                                   struct device_attribute *attr,
397                                   const char *buf, size_t len)
399         struct lis302dl_chip *chip = dev_get_drvdata(dev);
401         mutex_lock(&chip->lock);
403         if (!strcmp(buf, "on\n"))
404                 lis302dl_power(chip, 1);
405         else if (!strcmp(buf, "off\n"))
406                 lis302dl_power(chip, 0);
408         mutex_unlock(&chip->lock);
410         return len;
413 static ssize_t lis302dl_show_rate(struct device *dev,
414                                   struct device_attribute *attr, char *buf)
416         u8 val;
417         int ret;
418         struct lis302dl_chip *chip = dev_get_drvdata(dev);
420         mutex_lock(&chip->lock);
421         val = lis302dl_read(chip->client, LIS302_CTRL_1);
422         ret = snprintf(buf, PAGE_SIZE, "%d\n",
423                        (val & LIS302_CTRL1_DR) ? 400 : 100);
424         mutex_unlock(&chip->lock);
425         return ret;
428 static ssize_t lis302dl_set_rate(struct device *dev,
429                                  struct device_attribute *attr, const char *buf,
430                                  size_t len)
432         struct lis302dl_chip *chip = dev_get_drvdata(dev);
433         u8 reg;
435         mutex_lock(&chip->lock);
436         reg = lis302dl_read(chip->client, LIS302_CTRL_1);
437         if (!strcmp(buf, "400\n")) {
438                 reg |= LIS302_CTRL1_DR;
439                 chip->sample_rate = 1;
440                 lis302dl_write(chip->client, LIS302_CTRL_1, reg);
441                 set_duration(chip->client, chip->sample_rate, chip->duration);
442         } else if (!strcmp(buf, "100\n")) {
443                 reg &= ~LIS302_CTRL1_DR;
444                 chip->sample_rate = 0;
445                 lis302dl_write(chip->client, LIS302_CTRL_1, reg);
446                 set_duration(chip->client, chip->sample_rate, chip->duration);
448         }
449         mutex_unlock(&chip->lock);
451         return len;
454 static ssize_t lis302dl_show_scale(struct device *dev,
455                                    struct device_attribute *attr, char *buf)
457         int val, ret;
458         struct lis302dl_chip *chip = dev_get_drvdata(dev);
460         mutex_lock(&chip->lock);
461         val = lis302dl_read(chip->client, LIS302_CTRL_1);
463         if (val >= 0)
464                 if (val & LIS302_CTRL1_FS)
465                         ret = snprintf(buf, PAGE_SIZE, "full\n");
466                 else
467                         ret = snprintf(buf, PAGE_SIZE, "normal\n");
468         else
469                 ret = val;
470         mutex_unlock(&chip->lock);
472         return ret;
475 static ssize_t lis302dl_set_scale(struct device *dev,
476                                   struct device_attribute *attr,
477                                   const char *buf, size_t len)
479         struct lis302dl_chip *chip = dev_get_drvdata(dev);
480         u8 reg;
482         mutex_lock(&chip->lock);
483         reg = lis302dl_read(chip->client, LIS302_CTRL_1);
485         if (!strcmp(buf, "full\n")) {
486                 reg |= LIS302_CTRL1_FS;
487                 chip->fs = 1;
488                 lis302dl_write(chip->client, LIS302_CTRL_1, reg);
489                 set_ths(chip->client, chip->fs, chip->threshold);
490         } else if (!strcmp(buf, "normal\n")) {
491                 reg &= ~LIS302_CTRL1_FS;
492                 chip->fs = 0;
493                 lis302dl_write(chip->client, LIS302_CTRL_1, reg);
494                 set_ths(chip->client, chip->fs, chip->threshold);
495         }
496         mutex_unlock(&chip->lock);
498         return len;
501 static ssize_t lis302dl_show_duration(struct device *dev,
502                                       struct device_attribute *attr,
503                                       char *buf)
505         int val;
506         int ret;
507         struct lis302dl_chip *chip = dev_get_drvdata(dev);
509         mutex_lock(&chip->lock);
510         val = lis302dl_read(chip->client, LIS302_FF_WU_DURATION_1);
512         if (val >= 0)
513                 ret = snprintf(buf, PAGE_SIZE, "%d ms\n",
514                                chip->sample_rate ? (val * 10 / 4) : (val * 10));
515         else
516                 ret = val;
517         mutex_unlock(&chip->lock);
518         return ret;
521 static ssize_t lis302dl_set_duration(struct device *dev,
522                                      struct device_attribute *attr,
523                                      const char *buf, size_t len)
525         struct lis302dl_chip *chip = dev_get_drvdata(dev);
526         unsigned long duration;
527         int ret;
529         ret = strict_strtoul(buf, 0, &duration);
530         if (ret || duration < 0)
531                 return -EINVAL;
532         mutex_lock(&chip->lock);
533         /* max duration is 2.55 s when data rate is 100Hz */
534         if (duration > 2550)
535                 duration = 2550;
536         set_duration(chip->client, chip->sample_rate, duration);
537         chip->duration = duration;
538         mutex_unlock(&chip->lock);
539         return len;
542 static ssize_t lis302dl_show_ths(struct device *dev,
543                                  struct device_attribute *attr, char *buf)
545         int val, ret;
546         struct lis302dl_chip *chip = dev_get_drvdata(dev);
548         mutex_lock(&chip->lock);
549         val = lis302dl_read(chip->client, LIS302_FF_THS_1);
551         if (val >= 0)
552                 ret = snprintf(buf, PAGE_SIZE, "%d mg\n", val * (chip->fs ?
553                                       LIS302_BIG_UNIT : LIS302_SMALL_UNIT));
554         else
555                 ret = val;
556         mutex_unlock(&chip->lock);
557         return ret;
560 static ssize_t lis302dl_set_ths(struct device *dev,
561                                 struct device_attribute *attr,
562                                 const char *buf, size_t len)
564         struct lis302dl_chip *chip = dev_get_drvdata(dev);
565         unsigned long ths;
566         int ret;
568         ret = strict_strtoul(buf, 0, &ths);
569         if (ret)
570                 return -EINVAL;
571         mutex_lock(&chip->lock);
572         chip->threshold = ths;
573         set_ths(chip->client, chip->fs, chip->threshold);
574         mutex_unlock(&chip->lock);
576         return len;
579 static ssize_t lis302dl_show_samples(struct device *dev,
580                                      struct device_attribute *attr, char *buf)
582         int ret;
583         struct lis302dl_chip *chip = dev_get_drvdata(dev);
585         mutex_lock(&chip->lock);
586         ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->samples);
588         mutex_unlock(&chip->lock);
589         return ret;
592 static ssize_t lis302dl_set_samples(struct device *dev,
593                                     struct device_attribute *attr,
594                                     const char *buf, size_t len)
596         struct lis302dl_chip *chip = dev_get_drvdata(dev);
597         unsigned long samples;
598         int ret;
600         ret = strict_strtoul(buf, 0, &samples);
601         if (ret ||  samples < 1)
602                 return -EINVAL;
604         mutex_lock(&chip->lock);
605         chip->samples = samples;
606         mutex_unlock(&chip->lock);
608         return len;
611 static ssize_t lis302dl_show_coord(struct device *dev,
612                                    struct device_attribute *attr, char *buf)
614         struct lis302dl_chip *chip = dev_get_drvdata(dev);
615         int ret, i;
616         int x, y, z;
618         x = y = z = 0;
620         /* Cannot cancel synchronously within the mutex */
621         cancel_delayed_work_sync(&chip->poweroff_work);
623         mutex_lock(&chip->lock);
625         if (!chip->power)
626                 ret = lis302dl_power(chip, 1);
628         for (i = 0; i < chip->samples; i++) {
629                 x += (s8)lis302dl_read(chip->client, LIS302_X);
630                 y += (s8)lis302dl_read(chip->client, LIS302_Y);
631                 z += (s8)lis302dl_read(chip->client, LIS302_Z);
632         }
633         x /= (int)chip->samples;
634         y /= (int)chip->samples;
635         z /= (int)chip->samples;
637         /* convert to mg */
638         x *= (chip->fs ?  LIS302_BIG_UNIT : LIS302_SMALL_UNIT);
639         y *= (chip->fs ?  LIS302_BIG_UNIT : LIS302_SMALL_UNIT);
640         z *= (chip->fs ?  LIS302_BIG_UNIT : LIS302_SMALL_UNIT);
641         ret = snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);
642         mutex_unlock(&chip->lock);
644         schedule_delayed_work(&chip->poweroff_work, LIS302_POWEROFF_DELAY);
646         return ret;
649 static ssize_t lis302dl_show_selftest(struct device *dev,
650                                       struct device_attribute *attr, char *buf)
652         int ret;
654         struct lis302dl_chip *chip = dev_get_drvdata(dev);
656         mutex_lock(&chip->lock);
657         if (!lis302dl_selftest(chip))
658                 ret = snprintf(buf, PAGE_SIZE, "OK\n");
659         else
660                 ret = snprintf(buf, PAGE_SIZE, "FAIL\n");
661         mutex_unlock(&chip->lock);
663         return ret;
666 static struct device_attribute lis302dl_attrs[] = {
667         __ATTR(enable, S_IRUGO|S_IWUSR,
668                lis302dl_show_power, lis302dl_set_power),
669         __ATTR(rate, S_IRUGO|S_IWUSR,
670                lis302dl_show_rate, lis302dl_set_rate),
671         __ATTR(scale, S_IRUGO|S_IWUSR,
672                lis302dl_show_scale, lis302dl_set_scale),
673         __ATTR(duration, S_IRUGO|S_IWUSR,
674                lis302dl_show_duration, lis302dl_set_duration),
675         __ATTR(ths, S_IRUGO|S_IWUSR,
676                lis302dl_show_ths, lis302dl_set_ths),
677         __ATTR(samples, S_IRUGO|S_IWUSR,
678                lis302dl_show_samples, lis302dl_set_samples),
679         __ATTR(coord, S_IRUGO|S_IWUSR,
680                lis302dl_show_coord, NULL),
681         __ATTR(selftest, S_IRUGO|S_IWUSR,
682                lis302dl_show_selftest, NULL),
683 };
685 static int lis302dl_register_sysfs(struct i2c_client *c)
687         struct device *d = &c->dev;
688         int r, i;
690         for (i = 0; i < ARRAY_SIZE(lis302dl_attrs); i++) {
691                 r = device_create_file(d, &lis302dl_attrs[i]);
692                 if (r)
693                         goto fail;
694         }
695         return 0;
696 fail:
697         while (i--)
698                 device_remove_file(d, &lis302dl_attrs[i]);
700         return r;
703 static void lis302dl_unregister_sysfs(struct i2c_client *c)
705         struct device *d = &c->dev;
706         int i;
708         for (i = ARRAY_SIZE(lis302dl_attrs) - 1; i >= 0; i--)
709                 device_remove_file(d, &lis302dl_attrs[i]);
712 /*******************************************************************************
713  * INIT
714  ******************************************************************************/
715 static struct i2c_driver lis302dl_i2c_driver;
717 static int lis302dl_probe(struct i2c_client *client,
718                           const struct i2c_device_id *id)
720         struct lis302dl_chip *lis;
721         struct lis302dl_platform_data *pdata = client->dev.platform_data;
722         int err = 0;
724         if (!pdata) {
725                 dev_dbg(&client->dev, "no platform data?\n");
726                 return -EINVAL;
727         }
728         lis = kzalloc(sizeof(struct lis302dl_chip), GFP_KERNEL);
729         if (!lis)
730                 return -ENOMEM;
732         i2c_set_clientdata(client, lis);
733         lis->client = client;
735         err = lis302dl_detect(client);
736         if (err)
737                 goto fail2;
739         /* default startup values */
740         lis->power      = 1;
741         lis->threshold  = LIS302_THS;
742         lis->duration   = LIS302_DURATION;
743         lis->fs         = LIS302_FS;
744         lis->sample_rate = LIS302_100HZ;
745         lis->samples    = LIS302_SAMPLES;
747         mutex_init(&lis->lock);
749         err = lis302dl_configure(client);
750         if (err < 0) {
751                 dev_err(&client->dev, "lis302dl error configuring chip\n");
752                 goto fail2;
753         }
755         err = lis302dl_register_sysfs(client);
756         if (err) {
757                 printk(KERN_ALERT
758                        "lis302dl: sysfs registration failed, error %d\n", err);
759                 goto fail2;
760         }
762         lis->irq1 = pdata->int1_gpio;
763         lis->irq2 = pdata->int2_gpio;
765         /* gpio for interrupt pin 1 */
766         err = gpio_request(lis->irq1, "lis302dl_irq1");
767         if (err) {
768                 printk(KERN_ALERT "lis302dl: cannot request gpio for int 1\n");
769                 goto fail2;
770         }
771         gpio_direction_input(lis->irq1);
772         INIT_WORK(&lis->work1, lis302dl_work1);
773         INIT_DELAYED_WORK(&lis->poweroff_work, lis302dl_poweroff_work);
775         err = request_irq(gpio_to_irq(lis->irq1), lis302dl_irq1,
776                           LIS302_IRQ_FLAGS, DRIVER_NAME, lis);
777         if (err) {
778                 dev_err(&client->dev, "could not get IRQ_1 = %d\n",
779                         gpio_to_irq(lis->irq1));
780                 goto fail3;
781         }
782         schedule_delayed_work(&lis->poweroff_work, LIS302_POWEROFF_DELAY);
784         return 0;
786  fail3:
787         free_irq(gpio_to_irq(lis->irq1), lis);
788         gpio_free(lis->irq1);
789  fail2:
791         kfree(lis);
792         return err;
795 static int lis302dl_remove(struct i2c_client *client)
797         struct lis302dl_chip *chip = i2c_get_clientdata(client);
799         lis302dl_unregister_sysfs(client);
800         free_irq(gpio_to_irq(chip->irq1), chip);
801         gpio_free(chip->irq1);
803         cancel_delayed_work_sync(&chip->poweroff_work);
804         cancel_work_sync(&chip->work1);
805         lis302dl_power(chip, 0);
807         kfree(chip);
809         return 0;
812 static int lis302dl_suspend(struct i2c_client *client, pm_message_t state)
814         struct lis302dl_chip *chip = i2c_get_clientdata(client);
815         int ret;
817         mutex_lock(&chip->lock);
818         ret = lis302dl_power(chip, 0);
819         mutex_unlock(&chip->lock);
821         return ret;
824 static int lis302dl_resume(struct i2c_client *client)
826         struct lis302dl_chip *chip = i2c_get_clientdata(client);
827         int ret;
829         mutex_lock(&chip->lock);
830         ret = lis302dl_power(chip, 1);
831         mutex_unlock(&chip->lock);
833         return ret;
836 static const struct i2c_device_id lis302dl_id[] = {
837         { "lis302dl", 0 },
838         { }
839 };
840 MODULE_DEVICE_TABLE(i2c, lis302dl_id);
842 static struct i2c_driver lis302dl_i2c_driver = {
843         .driver = {
844                 .name    = DRIVER_NAME,
845         },
846         .suspend = lis302dl_suspend,
847         .resume = lis302dl_resume,
848         .probe  = lis302dl_probe,
849         .remove = lis302dl_remove,
850         .id_table = lis302dl_id,
851 };
853 static int __init lis302dl_init(void)
855         int ret;
857         ret = i2c_add_driver(&lis302dl_i2c_driver);
858         if (ret < 0)
859                 printk(KERN_ALERT "lis302dl driver registration failed\n");
861         return ret;
864 static void __exit lis302dl_exit(void)
866         i2c_del_driver(&lis302dl_i2c_driver);
869 MODULE_AUTHOR("Nokia Corporation");
870 MODULE_DESCRIPTION("LIS302DL acceleration sensor driver");
871 MODULE_LICENSE("GPL");
873 module_init(lis302dl_init);
874 module_exit(lis302dl_exit);

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment