Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2021 Google LLC.
0004  *
0005  * Common part of most Semtech SAR sensor.
0006  */
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/byteorder/generic.h>
0010 #include <linux/delay.h>
0011 #include <linux/device.h>
0012 #include <linux/err.h>
0013 #include <linux/export.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/irqreturn.h>
0016 #include <linux/i2c.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/regmap.h>
0020 #include <linux/regulator/consumer.h>
0021 #include <vdso/bits.h>
0022 
0023 #include <linux/iio/buffer.h>
0024 #include <linux/iio/events.h>
0025 #include <linux/iio/iio.h>
0026 #include <linux/iio/trigger.h>
0027 #include <linux/iio/triggered_buffer.h>
0028 #include <linux/iio/trigger_consumer.h>
0029 
0030 #include "sx_common.h"
0031 
0032 /* All Semtech SAR sensors have IRQ bit in the same order. */
0033 #define   SX_COMMON_CONVDONE_IRQ            BIT(0)
0034 #define   SX_COMMON_FAR_IRQ             BIT(2)
0035 #define   SX_COMMON_CLOSE_IRQ               BIT(3)
0036 
0037 const struct iio_event_spec sx_common_events[3] = {
0038     {
0039         .type = IIO_EV_TYPE_THRESH,
0040         .dir = IIO_EV_DIR_RISING,
0041         .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
0042     },
0043     {
0044         .type = IIO_EV_TYPE_THRESH,
0045         .dir = IIO_EV_DIR_FALLING,
0046         .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
0047     },
0048     {
0049         .type = IIO_EV_TYPE_THRESH,
0050         .dir = IIO_EV_DIR_EITHER,
0051         .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
0052                  BIT(IIO_EV_INFO_HYSTERESIS) |
0053                  BIT(IIO_EV_INFO_VALUE),
0054     },
0055 };
0056 EXPORT_SYMBOL_NS_GPL(sx_common_events, SEMTECH_PROX);
0057 
0058 static irqreturn_t sx_common_irq_handler(int irq, void *private)
0059 {
0060     struct iio_dev *indio_dev = private;
0061     struct sx_common_data *data = iio_priv(indio_dev);
0062 
0063     if (data->trigger_enabled)
0064         iio_trigger_poll(data->trig);
0065 
0066     /*
0067      * Even if no event is enabled, we need to wake the thread to clear the
0068      * interrupt state by reading SX_COMMON_REG_IRQ_SRC.
0069      * It is not possible to do that here because regmap_read takes a mutex.
0070      */
0071     return IRQ_WAKE_THREAD;
0072 }
0073 
0074 static void sx_common_push_events(struct iio_dev *indio_dev)
0075 {
0076     int ret;
0077     unsigned int val, chan;
0078     struct sx_common_data *data = iio_priv(indio_dev);
0079     s64 timestamp = iio_get_time_ns(indio_dev);
0080     unsigned long prox_changed;
0081 
0082     /* Read proximity state on all channels */
0083     ret = regmap_read(data->regmap, data->chip_info->reg_stat, &val);
0084     if (ret) {
0085         dev_err(&data->client->dev, "i2c transfer error in irq\n");
0086         return;
0087     }
0088 
0089     val >>= data->chip_info->stat_offset;
0090 
0091     /*
0092      * Only iterate over channels with changes on proximity status that have
0093      * events enabled.
0094      */
0095     prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
0096 
0097     for_each_set_bit(chan, &prox_changed, data->chip_info->num_channels) {
0098         int dir;
0099         u64 ev;
0100 
0101         dir = (val & BIT(chan)) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
0102         ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
0103                       IIO_EV_TYPE_THRESH, dir);
0104 
0105         iio_push_event(indio_dev, ev, timestamp);
0106     }
0107     data->chan_prox_stat = val;
0108 }
0109 
0110 static int sx_common_enable_irq(struct sx_common_data *data, unsigned int irq)
0111 {
0112     if (!data->client->irq)
0113         return 0;
0114     return regmap_update_bits(data->regmap, data->chip_info->reg_irq_msk,
0115                   irq << data->chip_info->irq_msk_offset,
0116                   irq << data->chip_info->irq_msk_offset);
0117 }
0118 
0119 static int sx_common_disable_irq(struct sx_common_data *data, unsigned int irq)
0120 {
0121     if (!data->client->irq)
0122         return 0;
0123     return regmap_update_bits(data->regmap, data->chip_info->reg_irq_msk,
0124                   irq << data->chip_info->irq_msk_offset, 0);
0125 }
0126 
0127 static int sx_common_update_chan_en(struct sx_common_data *data,
0128                     unsigned long chan_read,
0129                     unsigned long chan_event)
0130 {
0131     int ret;
0132     unsigned long channels = chan_read | chan_event;
0133 
0134     if ((data->chan_read | data->chan_event) != channels) {
0135         ret = regmap_update_bits(data->regmap,
0136                      data->chip_info->reg_enable_chan,
0137                      data->chip_info->mask_enable_chan,
0138                      channels);
0139         if (ret)
0140             return ret;
0141     }
0142     data->chan_read = chan_read;
0143     data->chan_event = chan_event;
0144     return 0;
0145 }
0146 
0147 static int sx_common_get_read_channel(struct sx_common_data *data, int channel)
0148 {
0149     return sx_common_update_chan_en(data, data->chan_read | BIT(channel),
0150                      data->chan_event);
0151 }
0152 
0153 static int sx_common_put_read_channel(struct sx_common_data *data, int channel)
0154 {
0155     return sx_common_update_chan_en(data, data->chan_read & ~BIT(channel),
0156                      data->chan_event);
0157 }
0158 
0159 static int sx_common_get_event_channel(struct sx_common_data *data, int channel)
0160 {
0161     return sx_common_update_chan_en(data, data->chan_read,
0162                      data->chan_event | BIT(channel));
0163 }
0164 
0165 static int sx_common_put_event_channel(struct sx_common_data *data, int channel)
0166 {
0167     return sx_common_update_chan_en(data, data->chan_read,
0168                      data->chan_event & ~BIT(channel));
0169 }
0170 
0171 /**
0172  * sx_common_read_proximity() - Read raw proximity value.
0173  * @data:   Internal data
0174  * @chan:   Channel to read
0175  * @val:    pointer to return read value.
0176  *
0177  * Request a conversion, wait for the sensor to be ready and
0178  * return the raw proximity value.
0179  */
0180 int sx_common_read_proximity(struct sx_common_data *data,
0181                  const struct iio_chan_spec *chan, int *val)
0182 {
0183     int ret;
0184     __be16 rawval;
0185 
0186     mutex_lock(&data->mutex);
0187 
0188     ret = sx_common_get_read_channel(data, chan->channel);
0189     if (ret)
0190         goto out;
0191 
0192     ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
0193     if (ret)
0194         goto out_put_channel;
0195 
0196     mutex_unlock(&data->mutex);
0197 
0198     if (data->client->irq) {
0199         ret = wait_for_completion_interruptible(&data->completion);
0200         reinit_completion(&data->completion);
0201     } else {
0202         ret = data->chip_info->ops.wait_for_sample(data);
0203     }
0204 
0205     mutex_lock(&data->mutex);
0206 
0207     if (ret)
0208         goto out_disable_irq;
0209 
0210     ret = data->chip_info->ops.read_prox_data(data, chan, &rawval);
0211     if (ret)
0212         goto out_disable_irq;
0213 
0214     *val = sign_extend32(be16_to_cpu(rawval), chan->scan_type.realbits - 1);
0215 
0216     ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
0217     if (ret)
0218         goto out_put_channel;
0219 
0220     ret = sx_common_put_read_channel(data, chan->channel);
0221     if (ret)
0222         goto out;
0223 
0224     mutex_unlock(&data->mutex);
0225 
0226     return IIO_VAL_INT;
0227 
0228 out_disable_irq:
0229     sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
0230 out_put_channel:
0231     sx_common_put_read_channel(data, chan->channel);
0232 out:
0233     mutex_unlock(&data->mutex);
0234 
0235     return ret;
0236 }
0237 EXPORT_SYMBOL_NS_GPL(sx_common_read_proximity, SEMTECH_PROX);
0238 
0239 /**
0240  * sx_common_read_event_config() - Configure event setting.
0241  * @indio_dev:  iio device object
0242  * @chan:   Channel to read
0243  * @type:   Type of event (unused)
0244  * @dir:    Direction of event (unused)
0245  *
0246  * return if the given channel is used for event gathering.
0247  */
0248 int sx_common_read_event_config(struct iio_dev *indio_dev,
0249                 const struct iio_chan_spec *chan,
0250                 enum iio_event_type type,
0251                 enum iio_event_direction dir)
0252 {
0253     struct sx_common_data *data = iio_priv(indio_dev);
0254 
0255     return !!(data->chan_event & BIT(chan->channel));
0256 }
0257 EXPORT_SYMBOL_NS_GPL(sx_common_read_event_config, SEMTECH_PROX);
0258 
0259 /**
0260  * sx_common_write_event_config() - Configure event setting.
0261  * @indio_dev:  iio device object
0262  * @chan:   Channel to enable
0263  * @type:   Type of event (unused)
0264  * @dir:    Direction of event (unused)
0265  * @state:  State of the event.
0266  *
0267  * Enable/Disable event on a given channel.
0268  */
0269 int sx_common_write_event_config(struct iio_dev *indio_dev,
0270                  const struct iio_chan_spec *chan,
0271                  enum iio_event_type type,
0272                  enum iio_event_direction dir, int state)
0273 {
0274     struct sx_common_data *data = iio_priv(indio_dev);
0275     unsigned int eventirq = SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ;
0276     int ret;
0277 
0278     /* If the state hasn't changed, there's nothing to do. */
0279     if (!!(data->chan_event & BIT(chan->channel)) == state)
0280         return 0;
0281 
0282     mutex_lock(&data->mutex);
0283     if (state) {
0284         ret = sx_common_get_event_channel(data, chan->channel);
0285         if (ret)
0286             goto out_unlock;
0287         if (!(data->chan_event & ~BIT(chan->channel))) {
0288             ret = sx_common_enable_irq(data, eventirq);
0289             if (ret)
0290                 sx_common_put_event_channel(data, chan->channel);
0291         }
0292     } else {
0293         ret = sx_common_put_event_channel(data, chan->channel);
0294         if (ret)
0295             goto out_unlock;
0296         if (!data->chan_event) {
0297             ret = sx_common_disable_irq(data, eventirq);
0298             if (ret)
0299                 sx_common_get_event_channel(data, chan->channel);
0300         }
0301     }
0302 
0303 out_unlock:
0304     mutex_unlock(&data->mutex);
0305     return ret;
0306 }
0307 EXPORT_SYMBOL_NS_GPL(sx_common_write_event_config, SEMTECH_PROX);
0308 
0309 static int sx_common_set_trigger_state(struct iio_trigger *trig, bool state)
0310 {
0311     struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
0312     struct sx_common_data *data = iio_priv(indio_dev);
0313     int ret = 0;
0314 
0315     mutex_lock(&data->mutex);
0316 
0317     if (state)
0318         ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
0319     else if (!data->chan_read)
0320         ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
0321     if (ret)
0322         goto out;
0323 
0324     data->trigger_enabled = state;
0325 
0326 out:
0327     mutex_unlock(&data->mutex);
0328 
0329     return ret;
0330 }
0331 
0332 static const struct iio_trigger_ops sx_common_trigger_ops = {
0333     .set_trigger_state = sx_common_set_trigger_state,
0334 };
0335 
0336 static irqreturn_t sx_common_irq_thread_handler(int irq, void *private)
0337 {
0338     struct iio_dev *indio_dev = private;
0339     struct sx_common_data *data = iio_priv(indio_dev);
0340     int ret;
0341     unsigned int val;
0342 
0343     mutex_lock(&data->mutex);
0344 
0345     ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val);
0346     if (ret) {
0347         dev_err(&data->client->dev, "i2c transfer error in irq\n");
0348         goto out;
0349     }
0350 
0351     if (val & ((SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ) << data->chip_info->irq_msk_offset))
0352         sx_common_push_events(indio_dev);
0353 
0354     if (val & (SX_COMMON_CONVDONE_IRQ << data->chip_info->irq_msk_offset))
0355         complete(&data->completion);
0356 
0357 out:
0358     mutex_unlock(&data->mutex);
0359 
0360     return IRQ_HANDLED;
0361 }
0362 
0363 static irqreturn_t sx_common_trigger_handler(int irq, void *private)
0364 {
0365     struct iio_poll_func *pf = private;
0366     struct iio_dev *indio_dev = pf->indio_dev;
0367     struct sx_common_data *data = iio_priv(indio_dev);
0368     __be16 val;
0369     int bit, ret, i = 0;
0370 
0371     mutex_lock(&data->mutex);
0372 
0373     for_each_set_bit(bit, indio_dev->active_scan_mask,
0374              indio_dev->masklength) {
0375         ret = data->chip_info->ops.read_prox_data(data,
0376                              &indio_dev->channels[bit],
0377                              &val);
0378         if (ret)
0379             goto out;
0380 
0381         data->buffer.channels[i++] = val;
0382     }
0383 
0384     iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
0385                        pf->timestamp);
0386 
0387 out:
0388     mutex_unlock(&data->mutex);
0389 
0390     iio_trigger_notify_done(indio_dev->trig);
0391 
0392     return IRQ_HANDLED;
0393 }
0394 
0395 static int sx_common_buffer_preenable(struct iio_dev *indio_dev)
0396 {
0397     struct sx_common_data *data = iio_priv(indio_dev);
0398     unsigned long channels = 0;
0399     int bit, ret;
0400 
0401     mutex_lock(&data->mutex);
0402     for_each_set_bit(bit, indio_dev->active_scan_mask,
0403              indio_dev->masklength)
0404         __set_bit(indio_dev->channels[bit].channel, &channels);
0405 
0406     ret = sx_common_update_chan_en(data, channels, data->chan_event);
0407     mutex_unlock(&data->mutex);
0408     return ret;
0409 }
0410 
0411 static int sx_common_buffer_postdisable(struct iio_dev *indio_dev)
0412 {
0413     struct sx_common_data *data = iio_priv(indio_dev);
0414     int ret;
0415 
0416     mutex_lock(&data->mutex);
0417     ret = sx_common_update_chan_en(data, 0, data->chan_event);
0418     mutex_unlock(&data->mutex);
0419     return ret;
0420 }
0421 
0422 static const struct iio_buffer_setup_ops sx_common_buffer_setup_ops = {
0423     .preenable = sx_common_buffer_preenable,
0424     .postdisable = sx_common_buffer_postdisable,
0425 };
0426 
0427 static void sx_common_regulator_disable(void *_data)
0428 {
0429     struct sx_common_data *data = _data;
0430 
0431     regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
0432 }
0433 
0434 #define SX_COMMON_SOFT_RESET                0xde
0435 
0436 static int sx_common_init_device(struct device *dev, struct iio_dev *indio_dev)
0437 {
0438     struct sx_common_data *data = iio_priv(indio_dev);
0439     struct sx_common_reg_default tmp;
0440     const struct sx_common_reg_default *initval;
0441     int ret;
0442     unsigned int i, val;
0443 
0444     ret = regmap_write(data->regmap, data->chip_info->reg_reset,
0445                SX_COMMON_SOFT_RESET);
0446     if (ret)
0447         return ret;
0448 
0449     usleep_range(1000, 2000); /* power-up time is ~1ms. */
0450 
0451     /* Clear reset interrupt state by reading SX_COMMON_REG_IRQ_SRC. */
0452     ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val);
0453     if (ret)
0454         return ret;
0455 
0456     /* Program defaults from constant or BIOS. */
0457     for (i = 0; i < data->chip_info->num_default_regs; i++) {
0458         initval = data->chip_info->ops.get_default_reg(dev, i, &tmp);
0459         ret = regmap_write(data->regmap, initval->reg, initval->def);
0460         if (ret)
0461             return ret;
0462     }
0463 
0464     return data->chip_info->ops.init_compensation(indio_dev);
0465 }
0466 
0467 /**
0468  * sx_common_probe() - Common setup for Semtech SAR sensor
0469  * @client:     I2C client object
0470  * @chip_info:      Semtech sensor chip information.
0471  * @regmap_config:  Sensor registers map configuration.
0472  */
0473 int sx_common_probe(struct i2c_client *client,
0474             const struct sx_common_chip_info *chip_info,
0475             const struct regmap_config *regmap_config)
0476 {
0477     struct device *dev = &client->dev;
0478     struct iio_dev *indio_dev;
0479     struct sx_common_data *data;
0480     int ret;
0481 
0482     indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
0483     if (!indio_dev)
0484         return -ENOMEM;
0485 
0486     data = iio_priv(indio_dev);
0487 
0488     data->chip_info = chip_info;
0489     data->client = client;
0490     data->supplies[0].supply = "vdd";
0491     data->supplies[1].supply = "svdd";
0492     mutex_init(&data->mutex);
0493     init_completion(&data->completion);
0494 
0495     data->regmap = devm_regmap_init_i2c(client, regmap_config);
0496     if (IS_ERR(data->regmap))
0497         return dev_err_probe(dev, PTR_ERR(data->regmap),
0498                      "Could init register map\n");
0499 
0500     ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
0501                       data->supplies);
0502     if (ret)
0503         return dev_err_probe(dev, ret, "Unable to get regulators\n");
0504 
0505     ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
0506     if (ret)
0507         return dev_err_probe(dev, ret, "Unable to enable regulators\n");
0508 
0509     /* Must wait for Tpor time after initial power up */
0510     usleep_range(1000, 1100);
0511 
0512     ret = devm_add_action_or_reset(dev, sx_common_regulator_disable, data);
0513     if (ret)
0514         return dev_err_probe(dev, ret,
0515                      "Unable to register regulators deleter\n");
0516 
0517     ret = data->chip_info->ops.check_whoami(dev, indio_dev);
0518     if (ret)
0519         return dev_err_probe(dev, ret, "error reading WHOAMI\n");
0520 
0521     indio_dev->modes = INDIO_DIRECT_MODE;
0522 
0523     indio_dev->channels =  data->chip_info->iio_channels;
0524     indio_dev->num_channels = data->chip_info->num_iio_channels;
0525     indio_dev->info = &data->chip_info->iio_info;
0526 
0527     i2c_set_clientdata(client, indio_dev);
0528 
0529     ret = sx_common_init_device(dev, indio_dev);
0530     if (ret)
0531         return dev_err_probe(dev, ret, "Unable to initialize sensor\n");
0532 
0533     if (client->irq) {
0534         ret = devm_request_threaded_irq(dev, client->irq,
0535                         sx_common_irq_handler,
0536                         sx_common_irq_thread_handler,
0537                         IRQF_ONESHOT,
0538                         "sx_event", indio_dev);
0539         if (ret)
0540             return dev_err_probe(dev, ret, "No IRQ\n");
0541 
0542         data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
0543                             indio_dev->name,
0544                             iio_device_id(indio_dev));
0545         if (!data->trig)
0546             return -ENOMEM;
0547 
0548         data->trig->ops = &sx_common_trigger_ops;
0549         iio_trigger_set_drvdata(data->trig, indio_dev);
0550 
0551         ret = devm_iio_trigger_register(dev, data->trig);
0552         if (ret)
0553             return ret;
0554     }
0555 
0556     ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
0557                           iio_pollfunc_store_time,
0558                           sx_common_trigger_handler,
0559                           &sx_common_buffer_setup_ops);
0560     if (ret)
0561         return ret;
0562 
0563     return devm_iio_device_register(dev, indio_dev);
0564 }
0565 EXPORT_SYMBOL_NS_GPL(sx_common_probe, SEMTECH_PROX);
0566 
0567 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
0568 MODULE_DESCRIPTION("Common functions and structures for Semtech sensor");
0569 MODULE_LICENSE("GPL v2");