23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
39 #define AK8975_REG_WIA 0x00
40 #define AK8975_DEVICE_ID 0x48
42 #define AK8975_REG_INFO 0x01
44 #define AK8975_REG_ST1 0x02
45 #define AK8975_REG_ST1_DRDY_SHIFT 0
46 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
48 #define AK8975_REG_HXL 0x03
49 #define AK8975_REG_HXH 0x04
50 #define AK8975_REG_HYL 0x05
51 #define AK8975_REG_HYH 0x06
52 #define AK8975_REG_HZL 0x07
53 #define AK8975_REG_HZH 0x08
54 #define AK8975_REG_ST2 0x09
55 #define AK8975_REG_ST2_DERR_SHIFT 2
56 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
58 #define AK8975_REG_ST2_HOFL_SHIFT 3
59 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
61 #define AK8975_REG_CNTL 0x0A
62 #define AK8975_REG_CNTL_MODE_SHIFT 0
63 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
64 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
65 #define AK8975_REG_CNTL_MODE_ONCE 1
66 #define AK8975_REG_CNTL_MODE_SELF_TEST 8
67 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
69 #define AK8975_REG_RSVC 0x0B
70 #define AK8975_REG_ASTC 0x0C
71 #define AK8975_REG_TS1 0x0D
72 #define AK8975_REG_TS2 0x0E
73 #define AK8975_REG_I2CDIS 0x0F
74 #define AK8975_REG_ASAX 0x10
75 #define AK8975_REG_ASAY 0x11
76 #define AK8975_REG_ASAZ 0x12
78 #define AK8975_MAX_REGS AK8975_REG_ASAZ
83 #define AK8975_MAX_CONVERSION_TIMEOUT 500
84 #define AK8975_CONVERSION_DONE_POLL_TIME 10
100 static const int ak8975_index_to_reg[] = {
110 struct iio_dev *indio_dev = i2c_get_clientdata(client);
115 regval = (data->
reg_cache[
reg] & ~mask) | (val << shift);
118 dev_err(&client->
dev,
"Write to device fails status %x\n", ret);
129 static int ak8975_read_data(
struct i2c_client *client,
135 .addr = client->
addr,
140 .addr = client->
addr,
149 dev_err(&client->
dev,
"Read from device fails\n");
160 static int ak8975_setup(
struct i2c_client *client)
162 struct iio_dev *indio_dev = i2c_get_clientdata(client);
174 dev_err(&client->
dev,
"Device ak8975 not found\n");
179 ret = ak8975_write_data(client,
185 dev_err(&client->
dev,
"Error in setting fuse access mode\n");
192 dev_err(&client->
dev,
"Not able to read asa data\n");
197 ret = ak8975_write_data(client,
203 dev_err(&client->
dev,
"Error in setting power-down mode\n");
249 static int wait_conversion_complete_gpio(
struct ak8975_data *data)
264 dev_err(&client->
dev,
"Conversion timeout happened\n");
270 dev_err(&client->
dev,
"Error in reading ST1\n");
276 static int wait_conversion_complete_polled(
struct ak8975_data *data)
288 dev_err(&client->
dev,
"Error in reading ST1\n");
296 dev_err(&client->
dev,
"Conversion timeout happened\n");
305 static int ak8975_read_axis(
struct iio_dev *indio_dev,
int index,
int *val)
317 ret = ak8975_write_data(client,
323 dev_err(&client->
dev,
"Error in setting operating mode\n");
329 ret = wait_conversion_complete_gpio(data);
331 ret = wait_conversion_complete_polled(data);
340 dev_err(&client->
dev,
"Error in reading ST2\n");
345 dev_err(&client->
dev,
"ST2 status error 0x%x\n",
354 ret = ak8975_read_data(client, ak8975_index_to_reg[index],
357 dev_err(&client->
dev,
"Read axis data fails\n");
376 static int ak8975_read_raw(
struct iio_dev *indio_dev,
385 return ak8975_read_axis(indio_dev, chan->
address, val);
393 #define AK8975_CHANNEL(axis, index) \
397 .channel2 = IIO_MOD_##axis, \
398 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
399 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
407 static const struct iio_info ak8975_info = {
408 .read_raw = &ak8975_read_raw,
421 if (client->
dev.platform_data ==
NULL)
424 eoc_gpio = *(
int *)(client->
dev.platform_data);
428 if (gpio_is_valid(eoc_gpio)) {
432 "failed to request GPIO %d, error %d\n",
440 if (indio_dev ==
NULL) {
444 data = iio_priv(indio_dev);
445 i2c_set_clientdata(client, indio_dev);
447 err = ak8975_setup(client);
449 dev_err(&client->
dev,
"AK8975 initialization fails\n");
457 indio_dev->
dev.parent = &client->
dev;
458 indio_dev->
channels = ak8975_channels;
460 indio_dev->
info = &ak8975_info;
472 if (gpio_is_valid(eoc_gpio))
480 struct iio_dev *indio_dev = i2c_get_clientdata(client);
501 { .compatible =
"asahi-kasei,ak8975", },
502 { .compatible =
"ak8975", },
510 .of_match_table = ak8975_of_match,
512 .probe = ak8975_probe,
514 .id_table = ak8975_id,