2 * VTI CMA3000_D0x Accelerometer driver
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Hemanth V <hemanthv@ti.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/types.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/input.h>
25 #include <linux/input/cma3000.h>
26 #include <linux/module.h>
28 #include "cma3000_d0x.h"
30 #define CMA3000_WHOAMI 0x00
31 #define CMA3000_REVID 0x01
32 #define CMA3000_CTRL 0x02
33 #define CMA3000_STATUS 0x03
34 #define CMA3000_RSTR 0x04
35 #define CMA3000_INTSTATUS 0x05
36 #define CMA3000_DOUTX 0x06
37 #define CMA3000_DOUTY 0x07
38 #define CMA3000_DOUTZ 0x08
39 #define CMA3000_MDTHR 0x09
40 #define CMA3000_MDFFTMR 0x0A
41 #define CMA3000_FFTHR 0x0B
43 #define CMA3000_RANGE2G (1 << 7)
44 #define CMA3000_RANGE8G (0 << 7)
45 #define CMA3000_BUSI2C (0 << 4)
46 #define CMA3000_MODEMASK (7 << 1)
47 #define CMA3000_GRANGEMASK (1 << 7)
49 #define CMA3000_STATUS_PERR 1
50 #define CMA3000_INTSTATUS_FFDET (1 << 2)
52 /* Settling time delay in ms */
53 #define CMA3000_SETDELAY 30
55 /* Delay for clearing interrupt in us */
56 #define CMA3000_INTDELAY 44
60 * Bit weights in mg for bit 0, other bits need
61 * multiply factor 2^n. Eight bit is the sign bit.
66 struct cma3000_accl_data
{
67 const struct cma3000_bus_ops
*bus_ops
;
68 const struct cma3000_platform_data
*pdata
;
71 struct input_dev
*input_dev
;
84 #define CMA3000_READ(data, reg, msg) \
85 (data->bus_ops->read(data->dev, reg, msg))
86 #define CMA3000_SET(data, reg, val, msg) \
87 ((data)->bus_ops->write(data->dev, reg, val, msg))
90 * Conversion for each of the eight modes to g, depending
91 * on G range i.e 2G or 8G. Some modes always operate in
95 static int mode_to_mg
[8][2] = {
97 { BIT_TO_8G
, BIT_TO_2G
},
98 { BIT_TO_8G
, BIT_TO_2G
},
99 { BIT_TO_8G
, BIT_TO_8G
},
100 { BIT_TO_8G
, BIT_TO_8G
},
101 { BIT_TO_8G
, BIT_TO_2G
},
102 { BIT_TO_8G
, BIT_TO_2G
},
106 static void decode_mg(struct cma3000_accl_data
*data
, int *datax
,
107 int *datay
, int *dataz
)
109 /* Data in 2's complement, convert to mg */
110 *datax
= ((s8
)*datax
) * data
->bit_to_mg
;
111 *datay
= ((s8
)*datay
) * data
->bit_to_mg
;
112 *dataz
= ((s8
)*dataz
) * data
->bit_to_mg
;
115 static irqreturn_t
cma3000_thread_irq(int irq
, void *dev_id
)
117 struct cma3000_accl_data
*data
= dev_id
;
118 int datax
, datay
, dataz
, intr_status
;
119 u8 ctrl
, mode
, range
;
121 intr_status
= CMA3000_READ(data
, CMA3000_INTSTATUS
, "interrupt status");
125 /* Check if free fall is detected, report immediately */
126 if (intr_status
& CMA3000_INTSTATUS_FFDET
) {
127 input_report_abs(data
->input_dev
, ABS_MISC
, 1);
128 input_sync(data
->input_dev
);
130 input_report_abs(data
->input_dev
, ABS_MISC
, 0);
133 datax
= CMA3000_READ(data
, CMA3000_DOUTX
, "X");
134 datay
= CMA3000_READ(data
, CMA3000_DOUTY
, "Y");
135 dataz
= CMA3000_READ(data
, CMA3000_DOUTZ
, "Z");
137 ctrl
= CMA3000_READ(data
, CMA3000_CTRL
, "ctrl");
138 mode
= (ctrl
& CMA3000_MODEMASK
) >> 1;
139 range
= (ctrl
& CMA3000_GRANGEMASK
) >> 7;
141 data
->bit_to_mg
= mode_to_mg
[mode
][range
];
143 /* Interrupt not for this device */
144 if (data
->bit_to_mg
== 0)
147 /* Decode register values to milli g */
148 decode_mg(data
, &datax
, &datay
, &dataz
);
150 input_report_abs(data
->input_dev
, ABS_X
, datax
);
151 input_report_abs(data
->input_dev
, ABS_Y
, datay
);
152 input_report_abs(data
->input_dev
, ABS_Z
, dataz
);
153 input_sync(data
->input_dev
);
158 static int cma3000_reset(struct cma3000_accl_data
*data
)
163 CMA3000_SET(data
, CMA3000_RSTR
, 0x02, "Reset");
164 CMA3000_SET(data
, CMA3000_RSTR
, 0x0A, "Reset");
165 CMA3000_SET(data
, CMA3000_RSTR
, 0x04, "Reset");
167 /* Settling time delay */
170 val
= CMA3000_READ(data
, CMA3000_STATUS
, "Status");
172 dev_err(data
->dev
, "Reset failed\n");
176 if (val
& CMA3000_STATUS_PERR
) {
177 dev_err(data
->dev
, "Parity Error\n");
184 static int cma3000_poweron(struct cma3000_accl_data
*data
)
186 const struct cma3000_platform_data
*pdata
= data
->pdata
;
190 if (data
->g_range
== CMARANGE_2G
) {
191 ctrl
= (data
->mode
<< 1) | CMA3000_RANGE2G
;
192 } else if (data
->g_range
== CMARANGE_8G
) {
193 ctrl
= (data
->mode
<< 1) | CMA3000_RANGE8G
;
196 "Invalid G range specified, assuming 8G\n");
197 ctrl
= (data
->mode
<< 1) | CMA3000_RANGE8G
;
200 ctrl
|= data
->bus_ops
->ctrl_mod
;
202 CMA3000_SET(data
, CMA3000_MDTHR
, pdata
->mdthr
,
203 "Motion Detect Threshold");
204 CMA3000_SET(data
, CMA3000_MDFFTMR
, pdata
->mdfftmr
,
206 CMA3000_SET(data
, CMA3000_FFTHR
, pdata
->ffthr
,
207 "Free fall threshold");
208 ret
= CMA3000_SET(data
, CMA3000_CTRL
, ctrl
, "Mode setting");
212 msleep(CMA3000_SETDELAY
);
217 static int cma3000_poweroff(struct cma3000_accl_data
*data
)
221 ret
= CMA3000_SET(data
, CMA3000_CTRL
, CMAMODE_POFF
, "Mode setting");
222 msleep(CMA3000_SETDELAY
);
227 static int cma3000_open(struct input_dev
*input_dev
)
229 struct cma3000_accl_data
*data
= input_get_drvdata(input_dev
);
231 mutex_lock(&data
->mutex
);
233 if (!data
->suspended
)
234 cma3000_poweron(data
);
238 mutex_unlock(&data
->mutex
);
243 static void cma3000_close(struct input_dev
*input_dev
)
245 struct cma3000_accl_data
*data
= input_get_drvdata(input_dev
);
247 mutex_lock(&data
->mutex
);
249 if (!data
->suspended
)
250 cma3000_poweroff(data
);
252 data
->opened
= false;
254 mutex_unlock(&data
->mutex
);
257 void cma3000_suspend(struct cma3000_accl_data
*data
)
259 mutex_lock(&data
->mutex
);
261 if (!data
->suspended
&& data
->opened
)
262 cma3000_poweroff(data
);
264 data
->suspended
= true;
266 mutex_unlock(&data
->mutex
);
268 EXPORT_SYMBOL(cma3000_suspend
);
271 void cma3000_resume(struct cma3000_accl_data
*data
)
273 mutex_lock(&data
->mutex
);
275 if (data
->suspended
&& data
->opened
)
276 cma3000_poweron(data
);
278 data
->suspended
= false;
280 mutex_unlock(&data
->mutex
);
282 EXPORT_SYMBOL(cma3000_resume
);
284 struct cma3000_accl_data
*cma3000_init(struct device
*dev
, int irq
,
285 const struct cma3000_bus_ops
*bops
)
287 const struct cma3000_platform_data
*pdata
= dev_get_platdata(dev
);
288 struct cma3000_accl_data
*data
;
289 struct input_dev
*input_dev
;
294 dev_err(dev
, "platform data not found\n");
300 /* if no IRQ return error */
306 data
= kzalloc(sizeof(struct cma3000_accl_data
), GFP_KERNEL
);
307 input_dev
= input_allocate_device();
308 if (!data
|| !input_dev
) {
314 data
->input_dev
= input_dev
;
315 data
->bus_ops
= bops
;
318 mutex_init(&data
->mutex
);
320 data
->mode
= pdata
->mode
;
321 if (data
->mode
> CMAMODE_POFF
) {
322 data
->mode
= CMAMODE_MOTDET
;
324 "Invalid mode specified, assuming Motion Detect\n");
327 data
->g_range
= pdata
->g_range
;
328 if (data
->g_range
!= CMARANGE_2G
&& data
->g_range
!= CMARANGE_8G
) {
330 "Invalid G range specified, assuming 8G\n");
331 data
->g_range
= CMARANGE_8G
;
334 input_dev
->name
= "cma3000-accelerometer";
335 input_dev
->id
.bustype
= bops
->bustype
;
336 input_dev
->open
= cma3000_open
;
337 input_dev
->close
= cma3000_close
;
339 __set_bit(EV_ABS
, input_dev
->evbit
);
341 input_set_abs_params(input_dev
, ABS_X
,
342 -data
->g_range
, data
->g_range
, pdata
->fuzz_x
, 0);
343 input_set_abs_params(input_dev
, ABS_Y
,
344 -data
->g_range
, data
->g_range
, pdata
->fuzz_y
, 0);
345 input_set_abs_params(input_dev
, ABS_Z
,
346 -data
->g_range
, data
->g_range
, pdata
->fuzz_z
, 0);
347 input_set_abs_params(input_dev
, ABS_MISC
, 0, 1, 0, 0);
349 input_set_drvdata(input_dev
, data
);
351 error
= cma3000_reset(data
);
355 rev
= CMA3000_READ(data
, CMA3000_REVID
, "Revid");
361 pr_info("CMA3000 Accelerometer: Revision %x\n", rev
);
363 error
= request_threaded_irq(irq
, NULL
, cma3000_thread_irq
,
364 pdata
->irqflags
| IRQF_ONESHOT
,
365 "cma3000_d0x", data
);
367 dev_err(dev
, "request_threaded_irq failed\n");
371 error
= input_register_device(data
->input_dev
);
373 dev_err(dev
, "Unable to register input device\n");
382 input_free_device(input_dev
);
385 return ERR_PTR(error
);
387 EXPORT_SYMBOL(cma3000_init
);
389 void cma3000_exit(struct cma3000_accl_data
*data
)
391 free_irq(data
->irq
, data
);
392 input_unregister_device(data
->input_dev
);
395 EXPORT_SYMBOL(cma3000_exit
);
397 MODULE_DESCRIPTION("CMA3000-D0x Accelerometer Driver");
398 MODULE_LICENSE("GPL");
399 MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");