Skip to content

Commit 3345d47

Browse files
richo25jic23
authored andcommitted
iio: magnetometer: mag3110: Add ability to run in continuous mode
Adds the ability to run the Mag3110 in continuous mode to speed up the sampling rate. Depending on the sampling rate requested the device can be put in or out of continuous mode automatically. Shifting out of continuous mode requires a potential 1 / ODR wait which is also implemented. Modified the sleep method when data is not ready to allow for sampling > 50sps to work. Signed-off-by: Richard Tresidder <rtresidd@electromag.com.au> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent e2fad74 commit 3345d47

File tree

1 file changed

+140
-18
lines changed

1 file changed

+140
-18
lines changed

drivers/iio/magnetometer/mag3110.c

Lines changed: 140 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define MAG3110_OUT_Y 0x03
2727
#define MAG3110_OUT_Z 0x05
2828
#define MAG3110_WHO_AM_I 0x07
29+
#define MAG3110_SYSMOD 0x08
2930
#define MAG3110_OFF_X 0x09 /* MSB first */
3031
#define MAG3110_OFF_Y 0x0b
3132
#define MAG3110_OFF_Z 0x0d
@@ -39,6 +40,8 @@
3940
#define MAG3110_CTRL_DR_SHIFT 5
4041
#define MAG3110_CTRL_DR_DEFAULT 0
4142

43+
#define MAG3110_SYSMOD_MODE_MASK GENMASK(1, 0)
44+
4245
#define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */
4346
#define MAG3110_CTRL_AC BIT(0) /* continuous measurements */
4447

@@ -52,17 +55,20 @@ struct mag3110_data {
5255
struct i2c_client *client;
5356
struct mutex lock;
5457
u8 ctrl_reg1;
58+
int sleep_val;
5559
};
5660

5761
static int mag3110_request(struct mag3110_data *data)
5862
{
5963
int ret, tries = 150;
6064

61-
/* trigger measurement */
62-
ret = i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
63-
data->ctrl_reg1 | MAG3110_CTRL_TM);
64-
if (ret < 0)
65-
return ret;
65+
if ((data->ctrl_reg1 & MAG3110_CTRL_AC) == 0) {
66+
/* trigger measurement */
67+
ret = i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
68+
data->ctrl_reg1 | MAG3110_CTRL_TM);
69+
if (ret < 0)
70+
return ret;
71+
}
6672

6773
while (tries-- > 0) {
6874
ret = i2c_smbus_read_byte_data(data->client, MAG3110_STATUS);
@@ -71,7 +77,11 @@ static int mag3110_request(struct mag3110_data *data)
7177
/* wait for data ready */
7278
if ((ret & MAG3110_STATUS_DRDY) == MAG3110_STATUS_DRDY)
7379
break;
74-
msleep(20);
80+
81+
if (data->sleep_val <= 20)
82+
usleep_range(data->sleep_val * 250, data->sleep_val * 500);
83+
else
84+
msleep(20);
7585
}
7686

7787
if (tries < 0) {
@@ -144,6 +154,117 @@ static int mag3110_get_samp_freq_index(struct mag3110_data *data,
144154
val2);
145155
}
146156

157+
static int mag3110_calculate_sleep(struct mag3110_data *data)
158+
{
159+
int ret, i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
160+
161+
if (mag3110_samp_freq[i][0] > 0)
162+
ret = 1000 / mag3110_samp_freq[i][0];
163+
else
164+
ret = 1000;
165+
166+
return ret == 0 ? 1 : ret;
167+
}
168+
169+
static int mag3110_standby(struct mag3110_data *data)
170+
{
171+
return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
172+
data->ctrl_reg1 & ~MAG3110_CTRL_AC);
173+
}
174+
175+
static int mag3110_wait_standby(struct mag3110_data *data)
176+
{
177+
int ret, tries = 30;
178+
179+
/*
180+
* Takes up to 1/ODR to come out of active mode into stby
181+
* Longest expected period is 12.5seconds.
182+
* We'll sleep for 500ms between checks
183+
*/
184+
while (tries-- > 0) {
185+
ret = i2c_smbus_read_byte_data(data->client, MAG3110_SYSMOD);
186+
if (ret < 0) {
187+
dev_err(&data->client->dev, "i2c error\n");
188+
return ret;
189+
}
190+
/* wait for standby */
191+
if ((ret & MAG3110_SYSMOD_MODE_MASK) == 0)
192+
break;
193+
194+
msleep_interruptible(500);
195+
}
196+
197+
if (tries < 0) {
198+
dev_err(&data->client->dev, "device not entering standby mode\n");
199+
return -EIO;
200+
}
201+
202+
return 0;
203+
}
204+
205+
static int mag3110_active(struct mag3110_data *data)
206+
{
207+
return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
208+
data->ctrl_reg1);
209+
}
210+
211+
/* returns >0 if active, 0 if in standby and <0 on error */
212+
static int mag3110_is_active(struct mag3110_data *data)
213+
{
214+
int reg;
215+
216+
reg = i2c_smbus_read_byte_data(data->client, MAG3110_CTRL_REG1);
217+
if (reg < 0)
218+
return reg;
219+
220+
return reg & MAG3110_CTRL_AC;
221+
}
222+
223+
static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
224+
{
225+
int ret;
226+
int is_active;
227+
228+
mutex_lock(&data->lock);
229+
230+
is_active = mag3110_is_active(data);
231+
if (is_active < 0) {
232+
ret = is_active;
233+
goto fail;
234+
}
235+
236+
/* config can only be changed when in standby */
237+
if (is_active > 0) {
238+
ret = mag3110_standby(data);
239+
if (ret < 0)
240+
goto fail;
241+
}
242+
243+
/*
244+
* After coming out of active we must wait for the part
245+
* to transition to STBY. This can take up to 1 /ODR to occur
246+
*/
247+
ret = mag3110_wait_standby(data);
248+
if (ret < 0)
249+
goto fail;
250+
251+
ret = i2c_smbus_write_byte_data(data->client, reg, val);
252+
if (ret < 0)
253+
goto fail;
254+
255+
if (is_active > 0) {
256+
ret = mag3110_active(data);
257+
if (ret < 0)
258+
goto fail;
259+
}
260+
261+
ret = 0;
262+
fail:
263+
mutex_unlock(&data->lock);
264+
265+
return ret;
266+
}
267+
147268
static int mag3110_read_raw(struct iio_dev *indio_dev,
148269
struct iio_chan_spec const *chan,
149270
int *val, int *val2, long mask)
@@ -235,11 +356,15 @@ static int mag3110_write_raw(struct iio_dev *indio_dev,
235356
ret = -EINVAL;
236357
break;
237358
}
238-
239-
data->ctrl_reg1 &= ~MAG3110_CTRL_DR_MASK;
359+
data->ctrl_reg1 &= 0xff & ~MAG3110_CTRL_DR_MASK
360+
& ~MAG3110_CTRL_AC;
240361
data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
241-
ret = i2c_smbus_write_byte_data(data->client,
242-
MAG3110_CTRL_REG1, data->ctrl_reg1);
362+
data->sleep_val = mag3110_calculate_sleep(data);
363+
if (data->sleep_val < 40)
364+
data->ctrl_reg1 |= MAG3110_CTRL_AC;
365+
366+
ret = mag3110_change_config(data, MAG3110_CTRL_REG1,
367+
data->ctrl_reg1);
243368
break;
244369
case IIO_CHAN_INFO_CALIBBIAS:
245370
if (val < -10000 || val > 10000) {
@@ -337,12 +462,6 @@ static const struct iio_info mag3110_info = {
337462

338463
static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0};
339464

340-
static int mag3110_standby(struct mag3110_data *data)
341-
{
342-
return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
343-
data->ctrl_reg1 & ~MAG3110_CTRL_AC);
344-
}
345-
346465
static int mag3110_probe(struct i2c_client *client,
347466
const struct i2c_device_id *id)
348467
{
@@ -374,8 +493,11 @@ static int mag3110_probe(struct i2c_client *client,
374493
indio_dev->available_scan_masks = mag3110_scan_masks;
375494

376495
data->ctrl_reg1 = MAG3110_CTRL_DR_DEFAULT << MAG3110_CTRL_DR_SHIFT;
377-
ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG1,
378-
data->ctrl_reg1);
496+
data->sleep_val = mag3110_calculate_sleep(data);
497+
if (data->sleep_val < 40)
498+
data->ctrl_reg1 |= MAG3110_CTRL_AC;
499+
500+
ret = mag3110_change_config(data, MAG3110_CTRL_REG1, data->ctrl_reg1);
379501
if (ret < 0)
380502
return ret;
381503

0 commit comments

Comments
 (0)