iio: magn: ak8975: allow a delay after enabling regulators
[cascardo/linux.git] / drivers / iio / magnetometer / ak8975.c
index 0e931a9..bf3ffc4 100644 (file)
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
 #include <linux/acpi.h>
+#include <linux/regulator/consumer.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#include <linux/iio/magnetometer/ak8975.h>
+
 /*
  * Register definitions, as well as various shifts and masks to get at the
  * individual fields of the registers.
@@ -361,7 +369,6 @@ static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
 struct ak8975_data {
        struct i2c_client       *client;
        const struct ak_def     *def;
-       struct attribute_group  attrs;
        struct mutex            lock;
        u8                      asa[3];
        long                    raw_to_gauss[3];
@@ -370,8 +377,44 @@ struct ak8975_data {
        wait_queue_head_t       data_ready_queue;
        unsigned long           flags;
        u8                      cntl_cache;
+       struct iio_mount_matrix orientation;
+       struct regulator        *vdd;
+       struct regulator        *vid;
 };
 
+/* Enable attached power regulator if any. */
+static int ak8975_power_on(const struct ak8975_data *data)
+{
+       int ret;
+
+       ret = regulator_enable(data->vdd);
+       if (ret) {
+               dev_warn(&data->client->dev,
+                        "Failed to enable specified Vdd supply\n");
+               return ret;
+       }
+       ret = regulator_enable(data->vid);
+       if (ret) {
+               dev_warn(&data->client->dev,
+                        "Failed to enable specified Vid supply\n");
+               return ret;
+       }
+       /*
+        * According to the datasheet the power supply rise time i 200us
+        * and the minimum wait time before mode setting is 100us, in
+        * total 300 us. Add some margin and say minimum 500us here.
+        */
+       usleep_range(500, 1000);
+       return 0;
+}
+
+/* Disable attached power regulator if any. */
+static void ak8975_power_off(const struct ak8975_data *data)
+{
+       regulator_disable(data->vid);
+       regulator_disable(data->vdd);
+}
+
 /*
  * Return 0 if the i2c device is the one we expect.
  * return a negative error number otherwise
@@ -390,8 +433,8 @@ static int ak8975_who_i_am(struct i2c_client *client,
         * AK8975   |  DEVICE_ID |  NA
         * AK8963   |  DEVICE_ID |  NA
         */
-       ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1,
-                                           2, wia_val);
+       ret = i2c_smbus_read_i2c_block_data_or_emulated(
+                       client, AK09912_REG_WIA1, 2, wia_val);
        if (ret < 0) {
                dev_err(&client->dev, "Error reading WIA\n");
                return ret;
@@ -503,9 +546,9 @@ static int ak8975_setup(struct i2c_client *client)
        }
 
        /* Get asa data and store in the device data. */
-       ret = i2c_smbus_read_i2c_block_data(client,
-                                           data->def->ctrl_regs[ASA_BASE],
-                                           3, data->asa);
+       ret = i2c_smbus_read_i2c_block_data_or_emulated(
+                       client, data->def->ctrl_regs[ASA_BASE],
+                       3, data->asa);
        if (ret < 0) {
                dev_err(&client->dev, "Not able to read asa data\n");
                return ret;
@@ -601,22 +644,15 @@ static int wait_conversion_complete_interrupt(struct ak8975_data *data)
        return ret > 0 ? 0 : -ETIME;
 }
 
-/*
- * Emits the raw flux value for the x, y, or z axis.
- */
-static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
+static int ak8975_start_read_axis(struct ak8975_data *data,
+                                 const struct i2c_client *client)
 {
-       struct ak8975_data *data = iio_priv(indio_dev);
-       struct i2c_client *client = data->client;
-       int ret;
-
-       mutex_lock(&data->lock);
-
        /* Set up the device for taking a sample. */
-       ret = ak8975_set_mode(data, MODE_ONCE);
+       int ret = ak8975_set_mode(data, MODE_ONCE);
+
        if (ret < 0) {
                dev_err(&client->dev, "Error in setting operating mode\n");
-               goto exit;
+               return ret;
        }
 
        /* Wait for the conversion to complete. */
@@ -627,7 +663,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
        else
                ret = wait_conversion_complete_polled(data);
        if (ret < 0)
-               goto exit;
+               return ret;
 
        /* This will be executed only for non-interrupt based waiting case */
        if (ret & data->def->ctrl_masks[ST1_DRDY]) {
@@ -635,32 +671,49 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
                                               data->def->ctrl_regs[ST2]);
                if (ret < 0) {
                        dev_err(&client->dev, "Error in reading ST2\n");
-                       goto exit;
+                       return ret;
                }
                if (ret & (data->def->ctrl_masks[ST2_DERR] |
                           data->def->ctrl_masks[ST2_HOFL])) {
                        dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
-                       ret = -EINVAL;
-                       goto exit;
+                       return -EINVAL;
                }
        }
 
-       /* Read the flux value from the appropriate register
-          (the register is specified in the iio device attributes). */
-       ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]);
-       if (ret < 0) {
-               dev_err(&client->dev, "Read axis data fails\n");
+       return 0;
+}
+
+/* Retrieve raw flux value for one of the x, y, or z axis.  */
+static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
+{
+       struct ak8975_data *data = iio_priv(indio_dev);
+       const struct i2c_client *client = data->client;
+       const struct ak_def *def = data->def;
+       u16 buff;
+       int ret;
+
+       mutex_lock(&data->lock);
+
+       ret = ak8975_start_read_axis(data, client);
+       if (ret)
+               goto exit;
+
+       ret = i2c_smbus_read_i2c_block_data_or_emulated(
+                       client, def->data_regs[index],
+                       sizeof(buff), (u8*)&buff);
+       if (ret < 0)
                goto exit;
-       }
 
        mutex_unlock(&data->lock);
 
-       /* Clamp to valid range. */
-       *val = clamp_t(s16, ret, -data->def->range, data->def->range);
+       /* Swap bytes and convert to valid range. */
+       buff = le16_to_cpu(buff);
+       *val = clamp_t(s16, buff, -def->range, def->range);
        return IIO_VAL_INT;
 
 exit:
        mutex_unlock(&data->lock);
+       dev_err(&client->dev, "Error in reading axis\n");
        return ret;
 }
 
@@ -682,6 +735,18 @@ static int ak8975_read_raw(struct iio_dev *indio_dev,
        return -EINVAL;
 }
 
+static const struct iio_mount_matrix *
+ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
+                       const struct iio_chan_spec *chan)
+{
+       return &((struct ak8975_data *)iio_priv(indio_dev))->orientation;
+}
+
+static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
+       IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
+       { },
+};
+
 #define AK8975_CHANNEL(axis, index)                                    \
        {                                                               \
                .type = IIO_MAGN,                                       \
@@ -690,12 +755,23 @@ static int ak8975_read_raw(struct iio_dev *indio_dev,
                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
                             BIT(IIO_CHAN_INFO_SCALE),                  \
                .address = index,                                       \
+               .scan_index = index,                                    \
+               .scan_type = {                                          \
+                       .sign = 's',                                    \
+                       .realbits = 16,                                 \
+                       .storagebits = 16,                              \
+                       .endianness = IIO_CPU                           \
+               },                                                      \
+               .ext_info = ak8975_ext_info,                            \
        }
 
 static const struct iio_chan_spec ak8975_channels[] = {
        AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
+       IIO_CHAN_SOFT_TIMESTAMP(3),
 };
 
+static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
+
 static const struct iio_info ak8975_info = {
        .read_raw = &ak8975_read_raw,
        .driver_module = THIS_MODULE,
@@ -724,6 +800,57 @@ static const char *ak8975_match_acpi_device(struct device *dev,
        return dev_name(dev);
 }
 
+static void ak8975_fill_buffer(struct iio_dev *indio_dev)
+{
+       struct ak8975_data *data = iio_priv(indio_dev);
+       const struct i2c_client *client = data->client;
+       const struct ak_def *def = data->def;
+       int ret;
+       s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
+
+       mutex_lock(&data->lock);
+
+       ret = ak8975_start_read_axis(data, client);
+       if (ret)
+               goto unlock;
+
+       /*
+        * For each axis, read the flux value from the appropriate register
+        * (the register is specified in the iio device attributes).
+        */
+       ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
+                                                       def->data_regs[0],
+                                                       3 * sizeof(buff[0]),
+                                                       (u8 *)buff);
+       if (ret < 0)
+               goto unlock;
+
+       mutex_unlock(&data->lock);
+
+       /* Clamp to valid range. */
+       buff[0] = clamp_t(s16, le16_to_cpu(buff[0]), -def->range, def->range);
+       buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range);
+       buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range);
+
+       iio_push_to_buffers_with_timestamp(indio_dev, buff,
+                                          iio_get_time_ns(indio_dev));
+       return;
+
+unlock:
+       mutex_unlock(&data->lock);
+       dev_err(&client->dev, "Error in reading axes block\n");
+}
+
+static irqreturn_t ak8975_handle_trigger(int irq, void *p)
+{
+       const struct iio_poll_func *pf = p;
+       struct iio_dev *indio_dev = pf->indio_dev;
+
+       ak8975_fill_buffer(indio_dev);
+       iio_trigger_notify_done(indio_dev->trig);
+       return IRQ_HANDLED;
+}
+
 static int ak8975_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -733,10 +860,12 @@ static int ak8975_probe(struct i2c_client *client,
        int err;
        const char *name = NULL;
        enum asahi_compass_chipset chipset = AK_MAX_TYPE;
+       const struct ak8975_platform_data *pdata =
+               dev_get_platdata(&client->dev);
 
        /* Grab and set up the supplied GPIO. */
-       if (client->dev.platform_data)
-               eoc_gpio = *(int *)(client->dev.platform_data);
+       if (pdata)
+               eoc_gpio = pdata->eoc_gpio;
        else if (client->dev.of_node)
                eoc_gpio = of_get_gpio(client->dev.of_node, 0);
        else
@@ -770,13 +899,24 @@ static int ak8975_probe(struct i2c_client *client,
        data->eoc_gpio = eoc_gpio;
        data->eoc_irq = 0;
 
+       if (!pdata) {
+               err = of_iio_read_mount_matrix(&client->dev,
+                                              "mount-matrix",
+                                              &data->orientation);
+               if (err)
+                       return err;
+       } else
+               data->orientation = pdata->orientation;
+
        /* id will be NULL when enumerated via ACPI */
        if (id) {
                chipset = (enum asahi_compass_chipset)(id->driver_data);
                name = id->name;
-       } else if (ACPI_HANDLE(&client->dev))
+       } else if (ACPI_HANDLE(&client->dev)) {
                name = ak8975_match_acpi_device(&client->dev, &chipset);
-       else
+               if (!name)
+                       return -ENODEV;
+       } else
                return -ENOSYS;
 
        if (chipset >= AK_MAX_TYPE) {
@@ -786,10 +926,23 @@ static int ak8975_probe(struct i2c_client *client,
        }
 
        data->def = &ak_def_array[chipset];
+
+       /* Fetch the regulators */
+       data->vdd = devm_regulator_get(&client->dev, "vdd");
+       if (IS_ERR(data->vdd))
+               return PTR_ERR(data->vdd);
+       data->vid = devm_regulator_get(&client->dev, "vid");
+       if (IS_ERR(data->vid))
+               return PTR_ERR(data->vid);
+
+       err = ak8975_power_on(data);
+       if (err)
+               return err;
+
        err = ak8975_who_i_am(client, data->def->type);
        if (err < 0) {
                dev_err(&client->dev, "Unexpected device\n");
-               return err;
+               goto power_off;
        }
        dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
 
@@ -797,7 +950,7 @@ static int ak8975_probe(struct i2c_client *client,
        err = ak8975_setup(client);
        if (err < 0) {
                dev_err(&client->dev, "%s initialization fails\n", name);
-               return err;
+               goto power_off;
        }
 
        mutex_init(&data->lock);
@@ -805,9 +958,42 @@ static int ak8975_probe(struct i2c_client *client,
        indio_dev->channels = ak8975_channels;
        indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
        indio_dev->info = &ak8975_info;
+       indio_dev->available_scan_masks = ak8975_scan_masks;
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->name = name;
-       return devm_iio_device_register(&client->dev, indio_dev);
+
+       err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
+                                        NULL);
+       if (err) {
+               dev_err(&client->dev, "triggered buffer setup failed\n");
+               goto power_off;
+       }
+
+       err = iio_device_register(indio_dev);
+       if (err) {
+               dev_err(&client->dev, "device register failed\n");
+               goto cleanup_buffer;
+       }
+
+       return 0;
+
+cleanup_buffer:
+       iio_triggered_buffer_cleanup(indio_dev);
+power_off:
+       ak8975_power_off(data);
+       return err;
+}
+
+static int ak8975_remove(struct i2c_client *client)
+{
+       struct iio_dev *indio_dev = i2c_get_clientdata(client);
+       struct ak8975_data *data = iio_priv(indio_dev);
+
+       iio_device_unregister(indio_dev);
+       iio_triggered_buffer_cleanup(indio_dev);
+       ak8975_power_off(data);
+
+       return 0;
 }
 
 static const struct i2c_device_id ak8975_id[] = {
@@ -841,6 +1027,7 @@ static struct i2c_driver ak8975_driver = {
                .acpi_match_table = ACPI_PTR(ak_acpi_match),
        },
        .probe          = ak8975_probe,
+       .remove         = ak8975_remove,
        .id_table       = ak8975_id,
 };
 module_i2c_driver(ak8975_driver);