spi: Use dev_get_drvdata at appropriate places
[deliverable/linux.git] / drivers / iio / pressure / st_pressure_core.c
1 /*
2 * STMicroelectronics pressures driver
3 *
4 * Copyright 2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/irq.h>
21 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/buffer.h>
26 #include <asm/unaligned.h>
27
28 #include <linux/iio/common/st_sensors.h>
29 #include "st_pressure.h"
30
31 #define ST_PRESS_MBAR_TO_KPASCAL(x) (x * 10)
32 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
33
34 /* DEFAULT VALUE FOR SENSORS */
35 #define ST_PRESS_DEFAULT_OUT_XL_ADDR 0x28
36 #define ST_TEMP_DEFAULT_OUT_L_ADDR 0x2b
37
38 /* FULLSCALE */
39 #define ST_PRESS_FS_AVL_1260MB 1260
40
41 /* CUSTOM VALUES FOR SENSOR 1 */
42 #define ST_PRESS_1_WAI_EXP 0xbb
43 #define ST_PRESS_1_ODR_ADDR 0x20
44 #define ST_PRESS_1_ODR_MASK 0x70
45 #define ST_PRESS_1_ODR_AVL_1HZ_VAL 0x01
46 #define ST_PRESS_1_ODR_AVL_7HZ_VAL 0x05
47 #define ST_PRESS_1_ODR_AVL_13HZ_VAL 0x06
48 #define ST_PRESS_1_ODR_AVL_25HZ_VAL 0x07
49 #define ST_PRESS_1_PW_ADDR 0x20
50 #define ST_PRESS_1_PW_MASK 0x80
51 #define ST_PRESS_1_FS_ADDR 0x23
52 #define ST_PRESS_1_FS_MASK 0x30
53 #define ST_PRESS_1_FS_AVL_1260_VAL 0x00
54 #define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_MBAR_TO_KPASCAL(244141)
55 #define ST_PRESS_1_FS_AVL_TEMP_GAIN 2083000
56 #define ST_PRESS_1_BDU_ADDR 0x20
57 #define ST_PRESS_1_BDU_MASK 0x04
58 #define ST_PRESS_1_DRDY_IRQ_ADDR 0x22
59 #define ST_PRESS_1_DRDY_IRQ_MASK 0x04
60 #define ST_PRESS_1_MULTIREAD_BIT true
61 #define ST_PRESS_1_TEMP_OFFSET 42500
62
63 static const struct iio_chan_spec st_press_channels[] = {
64 ST_SENSORS_LSM_CHANNELS(IIO_PRESSURE,
65 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
66 ST_SENSORS_SCAN_X, 0, IIO_NO_MOD, 'u', IIO_LE, 24, 24,
67 ST_PRESS_DEFAULT_OUT_XL_ADDR),
68 ST_SENSORS_LSM_CHANNELS(IIO_TEMP,
69 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
70 BIT(IIO_CHAN_INFO_OFFSET),
71 -1, 0, IIO_NO_MOD, 's', IIO_LE, 16, 16,
72 ST_TEMP_DEFAULT_OUT_L_ADDR),
73 IIO_CHAN_SOFT_TIMESTAMP(1)
74 };
75
76 static const struct st_sensors st_press_sensors[] = {
77 {
78 .wai = ST_PRESS_1_WAI_EXP,
79 .sensors_supported = {
80 [0] = LPS331AP_PRESS_DEV_NAME,
81 },
82 .ch = (struct iio_chan_spec *)st_press_channels,
83 .odr = {
84 .addr = ST_PRESS_1_ODR_ADDR,
85 .mask = ST_PRESS_1_ODR_MASK,
86 .odr_avl = {
87 { 1, ST_PRESS_1_ODR_AVL_1HZ_VAL, },
88 { 7, ST_PRESS_1_ODR_AVL_7HZ_VAL, },
89 { 13, ST_PRESS_1_ODR_AVL_13HZ_VAL, },
90 { 25, ST_PRESS_1_ODR_AVL_25HZ_VAL, },
91 },
92 },
93 .pw = {
94 .addr = ST_PRESS_1_PW_ADDR,
95 .mask = ST_PRESS_1_PW_MASK,
96 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
97 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
98 },
99 .fs = {
100 .addr = ST_PRESS_1_FS_ADDR,
101 .mask = ST_PRESS_1_FS_MASK,
102 .fs_avl = {
103 [0] = {
104 .num = ST_PRESS_FS_AVL_1260MB,
105 .value = ST_PRESS_1_FS_AVL_1260_VAL,
106 .gain = ST_PRESS_1_FS_AVL_1260_GAIN,
107 .gain2 = ST_PRESS_1_FS_AVL_TEMP_GAIN,
108 },
109 },
110 },
111 .bdu = {
112 .addr = ST_PRESS_1_BDU_ADDR,
113 .mask = ST_PRESS_1_BDU_MASK,
114 },
115 .drdy_irq = {
116 .addr = ST_PRESS_1_DRDY_IRQ_ADDR,
117 .mask = ST_PRESS_1_DRDY_IRQ_MASK,
118 },
119 .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT,
120 .bootime = 2,
121 },
122 };
123
124 static int st_press_read_raw(struct iio_dev *indio_dev,
125 struct iio_chan_spec const *ch, int *val,
126 int *val2, long mask)
127 {
128 int err;
129 struct st_sensor_data *pdata = iio_priv(indio_dev);
130
131 switch (mask) {
132 case IIO_CHAN_INFO_RAW:
133 err = st_sensors_read_info_raw(indio_dev, ch, val);
134 if (err < 0)
135 goto read_error;
136
137 return IIO_VAL_INT;
138 case IIO_CHAN_INFO_SCALE:
139 *val = 0;
140
141 switch (ch->type) {
142 case IIO_PRESSURE:
143 *val2 = pdata->current_fullscale->gain;
144 break;
145 case IIO_TEMP:
146 *val2 = pdata->current_fullscale->gain2;
147 break;
148 default:
149 err = -EINVAL;
150 goto read_error;
151 }
152
153 return IIO_VAL_INT_PLUS_NANO;
154 case IIO_CHAN_INFO_OFFSET:
155 switch (ch->type) {
156 case IIO_TEMP:
157 *val = 425;
158 *val2 = 10;
159 break;
160 default:
161 err = -EINVAL;
162 goto read_error;
163 }
164
165 return IIO_VAL_FRACTIONAL;
166 default:
167 return -EINVAL;
168 }
169
170 read_error:
171 return err;
172 }
173
174 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
175 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
176
177 static struct attribute *st_press_attributes[] = {
178 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
179 &iio_dev_attr_sampling_frequency.dev_attr.attr,
180 NULL,
181 };
182
183 static const struct attribute_group st_press_attribute_group = {
184 .attrs = st_press_attributes,
185 };
186
187 static const struct iio_info press_info = {
188 .driver_module = THIS_MODULE,
189 .attrs = &st_press_attribute_group,
190 .read_raw = &st_press_read_raw,
191 };
192
193 #ifdef CONFIG_IIO_TRIGGER
194 static const struct iio_trigger_ops st_press_trigger_ops = {
195 .owner = THIS_MODULE,
196 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
197 };
198 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
199 #else
200 #define ST_PRESS_TRIGGER_OPS NULL
201 #endif
202
203 int st_press_common_probe(struct iio_dev *indio_dev)
204 {
205 int err;
206 struct st_sensor_data *pdata = iio_priv(indio_dev);
207
208 indio_dev->modes = INDIO_DIRECT_MODE;
209 indio_dev->info = &press_info;
210
211 err = st_sensors_check_device_support(indio_dev,
212 ARRAY_SIZE(st_press_sensors), st_press_sensors);
213 if (err < 0)
214 goto st_press_common_probe_error;
215
216 pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
217 pdata->multiread_bit = pdata->sensor->multi_read_bit;
218 indio_dev->channels = pdata->sensor->ch;
219 indio_dev->num_channels = ARRAY_SIZE(st_press_channels);
220
221 pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
222 &pdata->sensor->fs.fs_avl[0];
223 pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
224
225 err = st_sensors_init_sensor(indio_dev);
226 if (err < 0)
227 goto st_press_common_probe_error;
228
229 if (pdata->get_irq_data_ready(indio_dev) > 0) {
230 err = st_press_allocate_ring(indio_dev);
231 if (err < 0)
232 goto st_press_common_probe_error;
233
234 err = st_sensors_allocate_trigger(indio_dev,
235 ST_PRESS_TRIGGER_OPS);
236 if (err < 0)
237 goto st_press_probe_trigger_error;
238 }
239
240 err = iio_device_register(indio_dev);
241 if (err)
242 goto st_press_device_register_error;
243
244 return err;
245
246 st_press_device_register_error:
247 if (pdata->get_irq_data_ready(indio_dev) > 0)
248 st_sensors_deallocate_trigger(indio_dev);
249 st_press_probe_trigger_error:
250 if (pdata->get_irq_data_ready(indio_dev) > 0)
251 st_press_deallocate_ring(indio_dev);
252 st_press_common_probe_error:
253 return err;
254 }
255 EXPORT_SYMBOL(st_press_common_probe);
256
257 void st_press_common_remove(struct iio_dev *indio_dev)
258 {
259 struct st_sensor_data *pdata = iio_priv(indio_dev);
260
261 iio_device_unregister(indio_dev);
262 if (pdata->get_irq_data_ready(indio_dev) > 0) {
263 st_sensors_deallocate_trigger(indio_dev);
264 st_press_deallocate_ring(indio_dev);
265 }
266 iio_device_free(indio_dev);
267 }
268 EXPORT_SYMBOL(st_press_common_remove);
269
270 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
271 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
272 MODULE_LICENSE("GPL v2");
This page took 0.037589 seconds and 5 git commands to generate.