forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/iio/pressure/bmp280-core.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
34 * Copyright (c) 2012 Bosch Sensortec GmbH
....@@ -6,10 +7,6 @@
67 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
78 *
89 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
9
- *
10
- * This program is free software; you can redistribute it and/or modify
11
- * it under the terms of the GNU General Public License version 2 as
12
- * published by the Free Software Foundation.
1310 *
1411 * Datasheet:
1512 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
....@@ -77,6 +74,12 @@
7774 s8 H6;
7875 };
7976
77
+static const char *const bmp280_supply_names[] = {
78
+ "vddd", "vdda"
79
+};
80
+
81
+#define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names)
82
+
8083 struct bmp280_data {
8184 struct device *dev;
8285 struct mutex lock;
....@@ -88,8 +91,7 @@
8891 struct bmp180_calib bmp180;
8992 struct bmp280_calib bmp280;
9093 } calib;
91
- struct regulator *vddd;
92
- struct regulator *vdda;
94
+ struct regulator_bulk_data supplies[BMP280_NUM_SUPPLIES];
9395 unsigned int start_up_time; /* in microseconds */
9496
9597 /* log of base 2 of oversampling rate */
....@@ -151,6 +153,8 @@
151153 {
152154 int ret;
153155 unsigned int tmp;
156
+ __le16 l16;
157
+ __be16 b16;
154158 struct device *dev = data->dev;
155159 __le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2];
156160 __le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2];
....@@ -164,6 +168,9 @@
164168 return ret;
165169 }
166170
171
+ /* Toss the temperature calibration data into the entropy pool */
172
+ add_device_randomness(t_buf, sizeof(t_buf));
173
+
167174 calib->T1 = le16_to_cpu(t_buf[T1]);
168175 calib->T2 = le16_to_cpu(t_buf[T2]);
169176 calib->T3 = le16_to_cpu(t_buf[T3]);
....@@ -176,6 +183,9 @@
176183 "failed to read pressure calibration parameters\n");
177184 return ret;
178185 }
186
+
187
+ /* Toss the pressure calibration data into the entropy pool */
188
+ add_device_randomness(p_buf, sizeof(p_buf));
179189
180190 calib->P1 = le16_to_cpu(p_buf[P1]);
181191 calib->P2 = le16_to_cpu(p_buf[P2]);
....@@ -204,12 +214,12 @@
204214 }
205215 calib->H1 = tmp;
206216
207
- ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
217
+ ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &l16, 2);
208218 if (ret < 0) {
209219 dev_err(dev, "failed to read H2 comp value\n");
210220 return ret;
211221 }
212
- calib->H2 = sign_extend32(le16_to_cpu(tmp), 15);
222
+ calib->H2 = sign_extend32(le16_to_cpu(l16), 15);
213223
214224 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
215225 if (ret < 0) {
....@@ -218,20 +228,20 @@
218228 }
219229 calib->H3 = tmp;
220230
221
- ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
231
+ ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &b16, 2);
222232 if (ret < 0) {
223233 dev_err(dev, "failed to read H4 comp value\n");
224234 return ret;
225235 }
226
- calib->H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
227
- (be16_to_cpu(tmp) & 0xf), 11);
236
+ calib->H4 = sign_extend32(((be16_to_cpu(b16) >> 4) & 0xff0) |
237
+ (be16_to_cpu(b16) & 0xf), 11);
228238
229
- ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
239
+ ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &l16, 2);
230240 if (ret < 0) {
231241 dev_err(dev, "failed to read H5 comp value\n");
232242 return ret;
233243 }
234
- calib->H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
244
+ calib->H5 = sign_extend32(((le16_to_cpu(l16) >> 4) & 0xfff), 11);
235245
236246 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
237247 if (ret < 0) {
....@@ -329,8 +339,7 @@
329339 __be32 tmp = 0;
330340 s32 adc_temp, comp_temp;
331341
332
- ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
333
- (u8 *) &tmp, 3);
342
+ ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, &tmp, 3);
334343 if (ret < 0) {
335344 dev_err(data->dev, "failed to read temperature\n");
336345 return ret;
....@@ -369,8 +378,7 @@
369378 if (ret < 0)
370379 return ret;
371380
372
- ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
373
- (u8 *) &tmp, 3);
381
+ ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, &tmp, 3);
374382 if (ret < 0) {
375383 dev_err(data->dev, "failed to read pressure\n");
376384 return ret;
....@@ -392,8 +400,8 @@
392400
393401 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
394402 {
403
+ __be16 tmp;
395404 int ret;
396
- __be16 tmp = 0;
397405 s32 adc_humidity;
398406 u32 comp_humidity;
399407
....@@ -402,8 +410,7 @@
402410 if (ret < 0)
403411 return ret;
404412
405
- ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
406
- (u8 *) &tmp, 2);
413
+ ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, &tmp, 2);
407414 if (ret < 0) {
408415 dev_err(data->dev, "failed to read humidity\n");
409416 return ret;
....@@ -567,57 +574,38 @@
567574 return ret;
568575 }
569576
570
-static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
577
+static int bmp280_read_avail(struct iio_dev *indio_dev,
578
+ struct iio_chan_spec const *chan,
579
+ const int **vals, int *type, int *length,
580
+ long mask)
571581 {
572
- size_t len = 0;
573
- int i;
582
+ struct bmp280_data *data = iio_priv(indio_dev);
574583
575
- for (i = 0; i < n; i++)
576
- len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
577
-
578
- buf[len - 1] = '\n';
579
-
580
- return len;
584
+ switch (mask) {
585
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
586
+ switch (chan->type) {
587
+ case IIO_PRESSURE:
588
+ *vals = data->chip_info->oversampling_press_avail;
589
+ *length = data->chip_info->num_oversampling_press_avail;
590
+ break;
591
+ case IIO_TEMP:
592
+ *vals = data->chip_info->oversampling_temp_avail;
593
+ *length = data->chip_info->num_oversampling_temp_avail;
594
+ break;
595
+ default:
596
+ return -EINVAL;
597
+ }
598
+ *type = IIO_VAL_INT;
599
+ return IIO_AVAIL_LIST;
600
+ default:
601
+ return -EINVAL;
602
+ }
581603 }
582
-
583
-static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
584
- struct device_attribute *attr, char *buf)
585
-{
586
- struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
587
-
588
- return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
589
- data->chip_info->num_oversampling_temp_avail);
590
-}
591
-
592
-static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
593
- struct device_attribute *attr, char *buf)
594
-{
595
- struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
596
-
597
- return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
598
- data->chip_info->num_oversampling_press_avail);
599
-}
600
-
601
-static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
602
- S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
603
-
604
-static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
605
- S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
606
-
607
-static struct attribute *bmp280_attributes[] = {
608
- &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
609
- &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
610
- NULL,
611
-};
612
-
613
-static const struct attribute_group bmp280_attrs_group = {
614
- .attrs = bmp280_attributes,
615
-};
616604
617605 static const struct iio_info bmp280_info = {
618606 .read_raw = &bmp280_read_raw,
607
+ .read_avail = &bmp280_read_avail,
619608 .write_raw = &bmp280_write_raw,
620
- .attrs = &bmp280_attrs_group,
621609 };
622610
623611 static int bmp280_chip_config(struct bmp280_data *data)
....@@ -744,14 +732,14 @@
744732
745733 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
746734 {
735
+ __be16 tmp;
747736 int ret;
748
- __be16 tmp = 0;
749737
750738 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
751739 if (ret)
752740 return ret;
753741
754
- ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
742
+ ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, &tmp, 2);
755743 if (ret)
756744 return ret;
757745
....@@ -848,7 +836,7 @@
848836 if (ret)
849837 return ret;
850838
851
- ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
839
+ ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, &tmp, 3);
852840 if (ret)
853841 return ret;
854842
....@@ -957,8 +945,7 @@
957945
958946 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
959947 if (irq_trig != IRQF_TRIGGER_RISING) {
960
- dev_err(dev, "non-rising trigger given for EOC interrupt, "
961
- "trying to enforce it\n");
948
+ dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
962949 irq_trig = IRQF_TRIGGER_RISING;
963950 }
964951
....@@ -981,6 +968,22 @@
981968 return 0;
982969 }
983970
971
+static void bmp280_pm_disable(void *data)
972
+{
973
+ struct device *dev = data;
974
+
975
+ pm_runtime_get_sync(dev);
976
+ pm_runtime_put_noidle(dev);
977
+ pm_runtime_disable(dev);
978
+}
979
+
980
+static void bmp280_regulators_disable(void *data)
981
+{
982
+ struct regulator_bulk_data *supplies = data;
983
+
984
+ regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
985
+}
986
+
984987 int bmp280_common_probe(struct device *dev,
985988 struct regmap *regmap,
986989 unsigned int chip,
....@@ -1001,7 +1004,6 @@
10011004 mutex_init(&data->lock);
10021005 data->dev = dev;
10031006
1004
- indio_dev->dev.parent = dev;
10051007 indio_dev->name = name;
10061008 indio_dev->channels = bmp280_channels;
10071009 indio_dev->info = &bmp280_info;
....@@ -1035,34 +1037,35 @@
10351037 }
10361038
10371039 /* Bring up regulators */
1038
- data->vddd = devm_regulator_get(dev, "vddd");
1039
- if (IS_ERR(data->vddd)) {
1040
- dev_err(dev, "failed to get VDDD regulator\n");
1041
- return PTR_ERR(data->vddd);
1042
- }
1043
- ret = regulator_enable(data->vddd);
1040
+ regulator_bulk_set_supply_names(data->supplies,
1041
+ bmp280_supply_names,
1042
+ BMP280_NUM_SUPPLIES);
1043
+
1044
+ ret = devm_regulator_bulk_get(dev,
1045
+ BMP280_NUM_SUPPLIES, data->supplies);
10441046 if (ret) {
1045
- dev_err(dev, "failed to enable VDDD regulator\n");
1047
+ dev_err(dev, "failed to get regulators\n");
10461048 return ret;
10471049 }
1048
- data->vdda = devm_regulator_get(dev, "vdda");
1049
- if (IS_ERR(data->vdda)) {
1050
- dev_err(dev, "failed to get VDDA regulator\n");
1051
- ret = PTR_ERR(data->vdda);
1052
- goto out_disable_vddd;
1053
- }
1054
- ret = regulator_enable(data->vdda);
1050
+
1051
+ ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
10551052 if (ret) {
1056
- dev_err(dev, "failed to enable VDDA regulator\n");
1057
- goto out_disable_vddd;
1053
+ dev_err(dev, "failed to enable regulators\n");
1054
+ return ret;
10581055 }
1056
+
1057
+ ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
1058
+ data->supplies);
1059
+ if (ret)
1060
+ return ret;
1061
+
10591062 /* Wait to make sure we started up properly */
10601063 usleep_range(data->start_up_time, data->start_up_time + 100);
10611064
10621065 /* Bring chip out of reset if there is an assigned GPIO line */
1063
- gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1066
+ gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
10641067 /* Deassert the signal */
1065
- if (!IS_ERR(gpiod)) {
1068
+ if (gpiod) {
10661069 dev_info(dev, "release reset\n");
10671070 gpiod_set_value(gpiod, 0);
10681071 }
....@@ -1070,17 +1073,16 @@
10701073 data->regmap = regmap;
10711074 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
10721075 if (ret < 0)
1073
- goto out_disable_vdda;
1076
+ return ret;
10741077 if (chip_id != chip) {
10751078 dev_err(dev, "bad chip id: expected %x got %x\n",
10761079 chip, chip_id);
1077
- ret = -EINVAL;
1078
- goto out_disable_vdda;
1080
+ return -EINVAL;
10791081 }
10801082
10811083 ret = data->chip_info->chip_config(data);
10821084 if (ret < 0)
1083
- goto out_disable_vdda;
1085
+ return ret;
10841086
10851087 dev_set_drvdata(dev, indio_dev);
10861088
....@@ -1094,14 +1096,14 @@
10941096 if (ret < 0) {
10951097 dev_err(data->dev,
10961098 "failed to read calibration coefficients\n");
1097
- goto out_disable_vdda;
1099
+ return ret;
10981100 }
10991101 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
11001102 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
11011103 if (ret < 0) {
11021104 dev_err(data->dev,
11031105 "failed to read calibration coefficients\n");
1104
- goto out_disable_vdda;
1106
+ return ret;
11051107 }
11061108 }
11071109
....@@ -1113,7 +1115,7 @@
11131115 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
11141116 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
11151117 if (ret)
1116
- goto out_disable_vdda;
1118
+ return ret;
11171119 }
11181120
11191121 /* Enable runtime PM */
....@@ -1128,51 +1130,21 @@
11281130 pm_runtime_use_autosuspend(dev);
11291131 pm_runtime_put(dev);
11301132
1131
- ret = iio_device_register(indio_dev);
1133
+ ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
11321134 if (ret)
1133
- goto out_runtime_pm_disable;
1135
+ return ret;
11341136
1135
-
1136
- return 0;
1137
-
1138
-out_runtime_pm_disable:
1139
- pm_runtime_get_sync(data->dev);
1140
- pm_runtime_put_noidle(data->dev);
1141
- pm_runtime_disable(data->dev);
1142
-out_disable_vdda:
1143
- regulator_disable(data->vdda);
1144
-out_disable_vddd:
1145
- regulator_disable(data->vddd);
1146
- return ret;
1137
+ return devm_iio_device_register(dev, indio_dev);
11471138 }
11481139 EXPORT_SYMBOL(bmp280_common_probe);
1149
-
1150
-int bmp280_common_remove(struct device *dev)
1151
-{
1152
- struct iio_dev *indio_dev = dev_get_drvdata(dev);
1153
- struct bmp280_data *data = iio_priv(indio_dev);
1154
-
1155
- iio_device_unregister(indio_dev);
1156
- pm_runtime_get_sync(data->dev);
1157
- pm_runtime_put_noidle(data->dev);
1158
- pm_runtime_disable(data->dev);
1159
- regulator_disable(data->vdda);
1160
- regulator_disable(data->vddd);
1161
- return 0;
1162
-}
1163
-EXPORT_SYMBOL(bmp280_common_remove);
11641140
11651141 #ifdef CONFIG_PM
11661142 static int bmp280_runtime_suspend(struct device *dev)
11671143 {
11681144 struct iio_dev *indio_dev = dev_get_drvdata(dev);
11691145 struct bmp280_data *data = iio_priv(indio_dev);
1170
- int ret;
11711146
1172
- ret = regulator_disable(data->vdda);
1173
- if (ret)
1174
- return ret;
1175
- return regulator_disable(data->vddd);
1147
+ return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
11761148 }
11771149
11781150 static int bmp280_runtime_resume(struct device *dev)
....@@ -1181,10 +1153,7 @@
11811153 struct bmp280_data *data = iio_priv(indio_dev);
11821154 int ret;
11831155
1184
- ret = regulator_enable(data->vddd);
1185
- if (ret)
1186
- return ret;
1187
- ret = regulator_enable(data->vdda);
1156
+ ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
11881157 if (ret)
11891158 return ret;
11901159 usleep_range(data->start_up_time, data->start_up_time + 100);