0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/delay.h>
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/io.h>
0014 #include <linux/clk.h>
0015 #include <linux/of.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/regulator/consumer.h>
0018 #include <linux/of_platform.h>
0019 #include <linux/err.h>
0020 #include <linux/pm_runtime.h>
0021
0022 #include <linux/iio/iio.h>
0023 #include <linux/iio/sysfs.h>
0024 #include <linux/iio/trigger.h>
0025
0026 #define DRIVER_NAME "rcar-gyroadc"
0027
0028
0029 #define RCAR_GYROADC_MODE_SELECT 0x00
0030 #define RCAR_GYROADC_MODE_SELECT_1_MB88101A 0x0
0031 #define RCAR_GYROADC_MODE_SELECT_2_ADCS7476 0x1
0032 #define RCAR_GYROADC_MODE_SELECT_3_MAX1162 0x3
0033
0034 #define RCAR_GYROADC_START_STOP 0x04
0035 #define RCAR_GYROADC_START_STOP_START BIT(0)
0036
0037 #define RCAR_GYROADC_CLOCK_LENGTH 0x08
0038 #define RCAR_GYROADC_1_25MS_LENGTH 0x0c
0039
0040 #define RCAR_GYROADC_REALTIME_DATA(ch) (0x10 + ((ch) * 4))
0041 #define RCAR_GYROADC_100MS_ADDED_DATA(ch) (0x30 + ((ch) * 4))
0042 #define RCAR_GYROADC_10MS_AVG_DATA(ch) (0x50 + ((ch) * 4))
0043
0044 #define RCAR_GYROADC_FIFO_STATUS 0x70
0045 #define RCAR_GYROADC_FIFO_STATUS_EMPTY(ch) BIT(0 + (4 * (ch)))
0046 #define RCAR_GYROADC_FIFO_STATUS_FULL(ch) BIT(1 + (4 * (ch)))
0047 #define RCAR_GYROADC_FIFO_STATUS_ERROR(ch) BIT(2 + (4 * (ch)))
0048
0049 #define RCAR_GYROADC_INTR 0x74
0050 #define RCAR_GYROADC_INTR_INT BIT(0)
0051
0052 #define RCAR_GYROADC_INTENR 0x78
0053 #define RCAR_GYROADC_INTENR_INTEN BIT(0)
0054
0055 #define RCAR_GYROADC_SAMPLE_RATE 800
0056
0057 #define RCAR_GYROADC_RUNTIME_PM_DELAY_MS 2000
0058
0059 enum rcar_gyroadc_model {
0060 RCAR_GYROADC_MODEL_DEFAULT,
0061 RCAR_GYROADC_MODEL_R8A7792,
0062 };
0063
0064 struct rcar_gyroadc {
0065 struct device *dev;
0066 void __iomem *regs;
0067 struct clk *clk;
0068 struct regulator *vref[8];
0069 unsigned int num_channels;
0070 enum rcar_gyroadc_model model;
0071 unsigned int mode;
0072 unsigned int sample_width;
0073 };
0074
0075 static void rcar_gyroadc_hw_init(struct rcar_gyroadc *priv)
0076 {
0077 const unsigned long clk_mhz = clk_get_rate(priv->clk) / 1000000;
0078 const unsigned long clk_mul =
0079 (priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) ? 10 : 5;
0080 unsigned long clk_len = clk_mhz * clk_mul;
0081
0082
0083
0084
0085
0086
0087 if (clk_len & 1)
0088 clk_len++;
0089
0090
0091 writel(0, priv->regs + RCAR_GYROADC_START_STOP);
0092
0093
0094 if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
0095 writel(0, priv->regs + RCAR_GYROADC_INTENR);
0096
0097
0098 writel(priv->mode, priv->regs + RCAR_GYROADC_MODE_SELECT);
0099 writel(clk_len, priv->regs + RCAR_GYROADC_CLOCK_LENGTH);
0100 writel(clk_mhz * 1250, priv->regs + RCAR_GYROADC_1_25MS_LENGTH);
0101 }
0102
0103 static void rcar_gyroadc_hw_start(struct rcar_gyroadc *priv)
0104 {
0105
0106 writel(RCAR_GYROADC_START_STOP_START,
0107 priv->regs + RCAR_GYROADC_START_STOP);
0108
0109
0110
0111
0112
0113
0114
0115 mdelay(3);
0116 }
0117
0118 static void rcar_gyroadc_hw_stop(struct rcar_gyroadc *priv)
0119 {
0120
0121 writel(0, priv->regs + RCAR_GYROADC_START_STOP);
0122 }
0123
0124 #define RCAR_GYROADC_CHAN(_idx) { \
0125 .type = IIO_VOLTAGE, \
0126 .indexed = 1, \
0127 .channel = (_idx), \
0128 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
0129 BIT(IIO_CHAN_INFO_SCALE), \
0130 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
0131 }
0132
0133 static const struct iio_chan_spec rcar_gyroadc_iio_channels_1[] = {
0134 RCAR_GYROADC_CHAN(0),
0135 RCAR_GYROADC_CHAN(1),
0136 RCAR_GYROADC_CHAN(2),
0137 RCAR_GYROADC_CHAN(3),
0138 };
0139
0140 static const struct iio_chan_spec rcar_gyroadc_iio_channels_2[] = {
0141 RCAR_GYROADC_CHAN(0),
0142 RCAR_GYROADC_CHAN(1),
0143 RCAR_GYROADC_CHAN(2),
0144 RCAR_GYROADC_CHAN(3),
0145 RCAR_GYROADC_CHAN(4),
0146 RCAR_GYROADC_CHAN(5),
0147 RCAR_GYROADC_CHAN(6),
0148 RCAR_GYROADC_CHAN(7),
0149 };
0150
0151 static const struct iio_chan_spec rcar_gyroadc_iio_channels_3[] = {
0152 RCAR_GYROADC_CHAN(0),
0153 RCAR_GYROADC_CHAN(1),
0154 RCAR_GYROADC_CHAN(2),
0155 RCAR_GYROADC_CHAN(3),
0156 RCAR_GYROADC_CHAN(4),
0157 RCAR_GYROADC_CHAN(5),
0158 RCAR_GYROADC_CHAN(6),
0159 RCAR_GYROADC_CHAN(7),
0160 };
0161
0162 static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
0163 {
0164 struct device *dev = priv->dev;
0165
0166 if (on) {
0167 return pm_runtime_resume_and_get(dev);
0168 } else {
0169 pm_runtime_mark_last_busy(dev);
0170 return pm_runtime_put_autosuspend(dev);
0171 }
0172 }
0173
0174 static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
0175 struct iio_chan_spec const *chan,
0176 int *val, int *val2, long mask)
0177 {
0178 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0179 struct regulator *consumer;
0180 unsigned int datareg = RCAR_GYROADC_REALTIME_DATA(chan->channel);
0181 unsigned int vref;
0182 int ret;
0183
0184
0185
0186
0187
0188 if (priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A)
0189 consumer = priv->vref[0];
0190 else
0191 consumer = priv->vref[chan->channel];
0192
0193 switch (mask) {
0194 case IIO_CHAN_INFO_RAW:
0195 if (chan->type != IIO_VOLTAGE)
0196 return -EINVAL;
0197
0198
0199 if (!consumer)
0200 return -EINVAL;
0201
0202 ret = iio_device_claim_direct_mode(indio_dev);
0203 if (ret)
0204 return ret;
0205
0206 ret = rcar_gyroadc_set_power(priv, true);
0207 if (ret < 0) {
0208 iio_device_release_direct_mode(indio_dev);
0209 return ret;
0210 }
0211
0212 *val = readl(priv->regs + datareg);
0213 *val &= BIT(priv->sample_width) - 1;
0214
0215 ret = rcar_gyroadc_set_power(priv, false);
0216 iio_device_release_direct_mode(indio_dev);
0217 if (ret < 0)
0218 return ret;
0219
0220 return IIO_VAL_INT;
0221 case IIO_CHAN_INFO_SCALE:
0222
0223 if (!consumer)
0224 return -EINVAL;
0225
0226 vref = regulator_get_voltage(consumer);
0227 *val = vref / 1000;
0228 *val2 = 1 << priv->sample_width;
0229
0230 return IIO_VAL_FRACTIONAL;
0231 case IIO_CHAN_INFO_SAMP_FREQ:
0232 *val = RCAR_GYROADC_SAMPLE_RATE;
0233
0234 return IIO_VAL_INT;
0235 default:
0236 return -EINVAL;
0237 }
0238 }
0239
0240 static int rcar_gyroadc_reg_access(struct iio_dev *indio_dev,
0241 unsigned int reg, unsigned int writeval,
0242 unsigned int *readval)
0243 {
0244 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0245 unsigned int maxreg = RCAR_GYROADC_FIFO_STATUS;
0246
0247 if (readval == NULL)
0248 return -EINVAL;
0249
0250 if (reg % 4)
0251 return -EINVAL;
0252
0253
0254 if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
0255 maxreg = RCAR_GYROADC_INTENR;
0256
0257 if (reg > maxreg)
0258 return -EINVAL;
0259
0260 *readval = readl(priv->regs + reg);
0261
0262 return 0;
0263 }
0264
0265 static const struct iio_info rcar_gyroadc_iio_info = {
0266 .read_raw = rcar_gyroadc_read_raw,
0267 .debugfs_reg_access = rcar_gyroadc_reg_access,
0268 };
0269
0270 static const struct of_device_id rcar_gyroadc_match[] = {
0271 {
0272
0273 .compatible = "renesas,rcar-gyroadc",
0274 .data = (void *)RCAR_GYROADC_MODEL_DEFAULT,
0275 }, {
0276
0277 .compatible = "renesas,r8a7792-gyroadc",
0278 .data = (void *)RCAR_GYROADC_MODEL_R8A7792,
0279 }, {
0280
0281 }
0282 };
0283
0284 MODULE_DEVICE_TABLE(of, rcar_gyroadc_match);
0285
0286 static const struct of_device_id rcar_gyroadc_child_match[] = {
0287
0288 {
0289 .compatible = "fujitsu,mb88101a",
0290 .data = (void *)RCAR_GYROADC_MODE_SELECT_1_MB88101A,
0291 },
0292
0293 {
0294 .compatible = "ti,adcs7476",
0295 .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
0296 }, {
0297 .compatible = "ti,adc121",
0298 .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
0299 }, {
0300 .compatible = "adi,ad7476",
0301 .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
0302 },
0303
0304 {
0305 .compatible = "maxim,max1162",
0306 .data = (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
0307 }, {
0308 .compatible = "maxim,max11100",
0309 .data = (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
0310 },
0311 { }
0312 };
0313
0314 static int rcar_gyroadc_parse_subdevs(struct iio_dev *indio_dev)
0315 {
0316 const struct of_device_id *of_id;
0317 const struct iio_chan_spec *channels;
0318 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0319 struct device *dev = priv->dev;
0320 struct device_node *np = dev->of_node;
0321 struct device_node *child;
0322 struct regulator *vref;
0323 unsigned int reg;
0324 unsigned int adcmode = -1, childmode;
0325 unsigned int sample_width;
0326 unsigned int num_channels;
0327 int ret, first = 1;
0328
0329 for_each_child_of_node(np, child) {
0330 of_id = of_match_node(rcar_gyroadc_child_match, child);
0331 if (!of_id) {
0332 dev_err(dev, "Ignoring unsupported ADC \"%pOFn\".",
0333 child);
0334 continue;
0335 }
0336
0337 childmode = (uintptr_t)of_id->data;
0338 switch (childmode) {
0339 case RCAR_GYROADC_MODE_SELECT_1_MB88101A:
0340 sample_width = 12;
0341 channels = rcar_gyroadc_iio_channels_1;
0342 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_1);
0343 break;
0344 case RCAR_GYROADC_MODE_SELECT_2_ADCS7476:
0345 sample_width = 15;
0346 channels = rcar_gyroadc_iio_channels_2;
0347 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_2);
0348 break;
0349 case RCAR_GYROADC_MODE_SELECT_3_MAX1162:
0350 sample_width = 16;
0351 channels = rcar_gyroadc_iio_channels_3;
0352 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_3);
0353 break;
0354 default:
0355 goto err_e_inval;
0356 }
0357
0358
0359
0360
0361
0362
0363
0364 if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
0365 reg = 0;
0366 } else {
0367 ret = of_property_read_u32(child, "reg", ®);
0368 if (ret) {
0369 dev_err(dev,
0370 "Failed to get child reg property of ADC \"%pOFn\".\n",
0371 child);
0372 goto err_of_node_put;
0373 }
0374
0375
0376 if (reg >= num_channels) {
0377 dev_err(dev,
0378 "Only %i channels supported with %pOFn, but reg = <%i>.\n",
0379 num_channels, child, reg);
0380 goto err_e_inval;
0381 }
0382 }
0383
0384
0385 if (!first && (adcmode != childmode)) {
0386 dev_err(dev,
0387 "Channel %i uses different ADC mode than the rest.\n",
0388 reg);
0389 goto err_e_inval;
0390 }
0391
0392
0393 dev->of_node = child;
0394 vref = devm_regulator_get(dev, "vref");
0395 dev->of_node = np;
0396 if (IS_ERR(vref)) {
0397 dev_dbg(dev, "Channel %i 'vref' supply not connected.\n",
0398 reg);
0399 ret = PTR_ERR(vref);
0400 goto err_of_node_put;
0401 }
0402
0403 priv->vref[reg] = vref;
0404
0405 if (!first)
0406 continue;
0407
0408
0409 adcmode = childmode;
0410 first = 0;
0411
0412 priv->num_channels = num_channels;
0413 priv->mode = childmode;
0414 priv->sample_width = sample_width;
0415
0416 indio_dev->channels = channels;
0417 indio_dev->num_channels = num_channels;
0418
0419
0420
0421
0422
0423
0424 if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
0425 of_node_put(child);
0426 break;
0427 }
0428 }
0429
0430 if (first) {
0431 dev_err(dev, "No valid ADC channels found, aborting.\n");
0432 return -EINVAL;
0433 }
0434
0435 return 0;
0436
0437 err_e_inval:
0438 ret = -EINVAL;
0439 err_of_node_put:
0440 of_node_put(child);
0441 return ret;
0442 }
0443
0444 static void rcar_gyroadc_deinit_supplies(struct iio_dev *indio_dev)
0445 {
0446 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0447 unsigned int i;
0448
0449 for (i = 0; i < priv->num_channels; i++) {
0450 if (!priv->vref[i])
0451 continue;
0452
0453 regulator_disable(priv->vref[i]);
0454 }
0455 }
0456
0457 static int rcar_gyroadc_init_supplies(struct iio_dev *indio_dev)
0458 {
0459 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0460 struct device *dev = priv->dev;
0461 unsigned int i;
0462 int ret;
0463
0464 for (i = 0; i < priv->num_channels; i++) {
0465 if (!priv->vref[i])
0466 continue;
0467
0468 ret = regulator_enable(priv->vref[i]);
0469 if (ret) {
0470 dev_err(dev, "Failed to enable regulator %i (ret=%i)\n",
0471 i, ret);
0472 goto err;
0473 }
0474 }
0475
0476 return 0;
0477
0478 err:
0479 rcar_gyroadc_deinit_supplies(indio_dev);
0480 return ret;
0481 }
0482
0483 static int rcar_gyroadc_probe(struct platform_device *pdev)
0484 {
0485 struct device *dev = &pdev->dev;
0486 struct rcar_gyroadc *priv;
0487 struct iio_dev *indio_dev;
0488 int ret;
0489
0490 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
0491 if (!indio_dev)
0492 return -ENOMEM;
0493
0494 priv = iio_priv(indio_dev);
0495 priv->dev = dev;
0496
0497 priv->regs = devm_platform_ioremap_resource(pdev, 0);
0498 if (IS_ERR(priv->regs))
0499 return PTR_ERR(priv->regs);
0500
0501 priv->clk = devm_clk_get(dev, "fck");
0502 if (IS_ERR(priv->clk))
0503 return dev_err_probe(dev, PTR_ERR(priv->clk),
0504 "Failed to get IF clock\n");
0505
0506 ret = rcar_gyroadc_parse_subdevs(indio_dev);
0507 if (ret)
0508 return ret;
0509
0510 ret = rcar_gyroadc_init_supplies(indio_dev);
0511 if (ret)
0512 return ret;
0513
0514 priv->model = (uintptr_t)of_device_get_match_data(&pdev->dev);
0515
0516 platform_set_drvdata(pdev, indio_dev);
0517
0518 indio_dev->name = DRIVER_NAME;
0519 indio_dev->info = &rcar_gyroadc_iio_info;
0520 indio_dev->modes = INDIO_DIRECT_MODE;
0521
0522 ret = clk_prepare_enable(priv->clk);
0523 if (ret) {
0524 dev_err(dev, "Could not prepare or enable the IF clock.\n");
0525 goto err_clk_if_enable;
0526 }
0527
0528 pm_runtime_set_autosuspend_delay(dev, RCAR_GYROADC_RUNTIME_PM_DELAY_MS);
0529 pm_runtime_use_autosuspend(dev);
0530 pm_runtime_enable(dev);
0531
0532 ret = pm_runtime_resume_and_get(dev);
0533 if (ret)
0534 goto err_power_up;
0535
0536 rcar_gyroadc_hw_init(priv);
0537 rcar_gyroadc_hw_start(priv);
0538
0539 ret = iio_device_register(indio_dev);
0540 if (ret) {
0541 dev_err(dev, "Couldn't register IIO device.\n");
0542 goto err_iio_device_register;
0543 }
0544
0545 pm_runtime_put_sync(dev);
0546
0547 return 0;
0548
0549 err_iio_device_register:
0550 rcar_gyroadc_hw_stop(priv);
0551 pm_runtime_put_sync(dev);
0552 err_power_up:
0553 pm_runtime_disable(dev);
0554 pm_runtime_set_suspended(dev);
0555 clk_disable_unprepare(priv->clk);
0556 err_clk_if_enable:
0557 rcar_gyroadc_deinit_supplies(indio_dev);
0558
0559 return ret;
0560 }
0561
0562 static int rcar_gyroadc_remove(struct platform_device *pdev)
0563 {
0564 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
0565 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0566 struct device *dev = priv->dev;
0567
0568 iio_device_unregister(indio_dev);
0569 pm_runtime_get_sync(dev);
0570 rcar_gyroadc_hw_stop(priv);
0571 pm_runtime_put_sync(dev);
0572 pm_runtime_disable(dev);
0573 pm_runtime_set_suspended(dev);
0574 clk_disable_unprepare(priv->clk);
0575 rcar_gyroadc_deinit_supplies(indio_dev);
0576
0577 return 0;
0578 }
0579
0580 static int rcar_gyroadc_suspend(struct device *dev)
0581 {
0582 struct iio_dev *indio_dev = dev_get_drvdata(dev);
0583 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0584
0585 rcar_gyroadc_hw_stop(priv);
0586
0587 return 0;
0588 }
0589
0590 static int rcar_gyroadc_resume(struct device *dev)
0591 {
0592 struct iio_dev *indio_dev = dev_get_drvdata(dev);
0593 struct rcar_gyroadc *priv = iio_priv(indio_dev);
0594
0595 rcar_gyroadc_hw_start(priv);
0596
0597 return 0;
0598 }
0599
0600 static const struct dev_pm_ops rcar_gyroadc_pm_ops = {
0601 RUNTIME_PM_OPS(rcar_gyroadc_suspend, rcar_gyroadc_resume, NULL)
0602 };
0603
0604 static struct platform_driver rcar_gyroadc_driver = {
0605 .probe = rcar_gyroadc_probe,
0606 .remove = rcar_gyroadc_remove,
0607 .driver = {
0608 .name = DRIVER_NAME,
0609 .of_match_table = rcar_gyroadc_match,
0610 .pm = pm_ptr(&rcar_gyroadc_pm_ops),
0611 },
0612 };
0613
0614 module_platform_driver(rcar_gyroadc_driver);
0615
0616 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
0617 MODULE_DESCRIPTION("Renesas R-Car GyroADC driver");
0618 MODULE_LICENSE("GPL");