1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics LSM9DS0 IMU driver
4  *
5  * Copyright (C) 2021, Intel Corporation
6  *
7  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8  */
9 
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <linux/iio/common/st_sensors.h>
17 #include <linux/iio/iio.h>
18 
19 #include "st_lsm9ds0.h"
20 
st_lsm9ds0_probe_accel(struct st_lsm9ds0 * lsm9ds0,struct regmap * regmap)21 static int st_lsm9ds0_probe_accel(struct st_lsm9ds0 *lsm9ds0, struct regmap *regmap)
22 {
23 	const struct st_sensor_settings *settings;
24 	struct device *dev = lsm9ds0->dev;
25 	struct st_sensor_data *data;
26 
27 	settings = st_accel_get_settings(lsm9ds0->name);
28 	if (!settings) {
29 		dev_err(dev, "device name %s not recognized.\n", lsm9ds0->name);
30 		return -ENODEV;
31 	}
32 
33 	lsm9ds0->accel = devm_iio_device_alloc(dev, sizeof(*data));
34 	if (!lsm9ds0->accel)
35 		return -ENOMEM;
36 
37 	lsm9ds0->accel->name = lsm9ds0->name;
38 
39 	data = iio_priv(lsm9ds0->accel);
40 	data->sensor_settings = (struct st_sensor_settings *)settings;
41 	data->irq = lsm9ds0->irq;
42 	data->regmap = regmap;
43 
44 	return st_accel_common_probe(lsm9ds0->accel);
45 }
46 
st_lsm9ds0_probe_magn(struct st_lsm9ds0 * lsm9ds0,struct regmap * regmap)47 static int st_lsm9ds0_probe_magn(struct st_lsm9ds0 *lsm9ds0, struct regmap *regmap)
48 {
49 	const struct st_sensor_settings *settings;
50 	struct device *dev = lsm9ds0->dev;
51 	struct st_sensor_data *data;
52 
53 	settings = st_magn_get_settings(lsm9ds0->name);
54 	if (!settings) {
55 		dev_err(dev, "device name %s not recognized.\n", lsm9ds0->name);
56 		return -ENODEV;
57 	}
58 
59 	lsm9ds0->magn = devm_iio_device_alloc(dev, sizeof(*data));
60 	if (!lsm9ds0->magn)
61 		return -ENOMEM;
62 
63 	lsm9ds0->magn->name = lsm9ds0->name;
64 
65 	data = iio_priv(lsm9ds0->magn);
66 	data->sensor_settings = (struct st_sensor_settings *)settings;
67 	data->irq = lsm9ds0->irq;
68 	data->regmap = regmap;
69 
70 	return st_magn_common_probe(lsm9ds0->magn);
71 }
72 
st_lsm9ds0_probe(struct st_lsm9ds0 * lsm9ds0,struct regmap * regmap)73 int st_lsm9ds0_probe(struct st_lsm9ds0 *lsm9ds0, struct regmap *regmap)
74 {
75 	struct device *dev = lsm9ds0->dev;
76 	static const char * const regulator_names[] = { "vdd", "vddio" };
77 	int ret;
78 
79 	/* Regulators not mandatory, but if requested we should enable them. */
80 	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
81 					     regulator_names);
82 	if (ret)
83 		return dev_err_probe(dev, ret,
84 				     "unable to enable Vdd supply\n");
85 
86 	/* Setup accelerometer device */
87 	ret = st_lsm9ds0_probe_accel(lsm9ds0, regmap);
88 	if (ret)
89 		return ret;
90 
91 	/* Setup magnetometer device */
92 	return st_lsm9ds0_probe_magn(lsm9ds0, regmap);
93 }
94 EXPORT_SYMBOL_NS_GPL(st_lsm9ds0_probe, IIO_ST_SENSORS);
95 
96 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
97 MODULE_DESCRIPTION("STMicroelectronics LSM9DS0 IMU core driver");
98 MODULE_LICENSE("GPL v2");
99 MODULE_IMPORT_NS(IIO_ST_SENSORS);
100