History

mfd / platform / iio: cros_ec: Register sensor through sensorhub

Remove the duplicated code in MFD, since MFD just registers cros-ec-sensorhub if at least one sensor is present. Change IIO cros-ec driver to get the pointer to the cros-ec-dev through cros-ec-sensorhub.

Gwendal Grignou committed 6 years ago

Commit d60ac881 parent 53067477 files · +23 −218Browse files

Unified · Split

7 files
  1. drivers/iio/accel/cros_ec_accel_legacy.c +0 −6
  2. drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c +0 −6
  3. drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 −1
  4. drivers/iio/light/cros_ec_light_prox.c +0 −6
  5. drivers/mfd/cros_ec_dev.c +12 −191
  6. include/linux/platform_data/cros_ec_proto.h +0 −8
  7. include/linux/platform_data/cros_ec_sensorhub.h +8 −0
drivers/iio/accel/cros_ec_accel_legacy.c+0−6
@@ -163,16 +163,10 @@ static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = { · 162 unmodified lines
163163 static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
164164 {
165165 struct device *dev = &pdev->dev;
166− struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
167166 struct iio_dev *indio_dev;
168167 struct cros_ec_sensors_core_state *state;
169168 int ret;
170169
171− if (!ec || !ec->ec_dev) {
172− dev_warn(&pdev->dev, "No EC device found.\n");
173− return -EINVAL;
174− }
175−
176170 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
177171 if (!indio_dev)
178172 return -ENOMEM;
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c+0−6
@@ -222,17 +222,11 @@ static const struct iio_info ec_sensors_info = { · 221 unmodified lines
222222 static int cros_ec_sensors_probe(struct platform_device *pdev)
223223 {
224224 struct device *dev = &pdev->dev;
225− struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
226225 struct iio_dev *indio_dev;
227226 struct cros_ec_sensors_state *state;
228227 struct iio_chan_spec *channel;
229228 int ret, i;
230229
231− if (!ec_dev || !ec_dev->ec_dev) {
232− dev_warn(&pdev->dev, "No CROS EC device found.\n");
233− return -EINVAL;
234− }
235−
236230 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
237231 if (!indio_dev)
238232 return -ENOMEM;
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c+3−1
@@ -18,6 +18,7 @@ · 17 unmodified lines
1818 #include <linux/slab.h>
1919 #include <linux/platform_data/cros_ec_commands.h>
2020 #include <linux/platform_data/cros_ec_proto.h>
21+#include <linux/platform_data/cros_ec_sensorhub.h>
2122 #include <linux/platform_device.h>
2223
2324 static char *cros_ec_loc[] = {
@@ -88,7 +89,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev, · 64 unmodified lines
8889 {
8990 struct device *dev = &pdev->dev;
9091 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
91− struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
92+ struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
93+ struct cros_ec_dev *ec = sensor_hub->ec;
9294 struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
9395 u32 ver_mask;
9496 int ret, i;
drivers/iio/light/cros_ec_light_prox.c+0−6
@@ -169,17 +169,11 @@ static const struct iio_info cros_ec_light_prox_info = { · 168 unmodified lines
169169 static int cros_ec_light_prox_probe(struct platform_device *pdev)
170170 {
171171 struct device *dev = &pdev->dev;
172− struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
173172 struct iio_dev *indio_dev;
174173 struct cros_ec_light_prox_state *state;
175174 struct iio_chan_spec *channel;
176175 int ret;
177176
178− if (!ec_dev || !ec_dev->ec_dev) {
179− dev_warn(dev, "No CROS EC device found.\n");
180− return -EINVAL;
181− }
182−
183177 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
184178 if (!indio_dev)
185179 return -ENOMEM;
drivers/mfd/cros_ec_dev.c+12−191
@@ -78,6 +78,10 @@ static const struct mfd_cell cros_ec_rtc_cells[] = { · 77 unmodified lines
7878 { .name = "cros-ec-rtc", },
7979 };
8080
81+static const struct mfd_cell cros_ec_sensorhub_cells[] = {
82+ { .name = "cros-ec-sensorhub", },
83+};
84+
8185 static const struct mfd_cell cros_usbpd_charger_cells[] = {
8286 { .name = "cros-usbpd-charger", },
8387 { .name = "cros-usbpd-logger", },
@@ -117,192 +121,6 @@ static void cros_ec_class_release(struct device *dev) · 33 unmodified lines
117121 kfree(to_cros_ec_dev(dev));
118122 }
119123
120−static void cros_ec_sensors_register(struct cros_ec_dev *ec)
121−{
122− /*
123− * Issue a command to get the number of sensor reported.
124− * Build an array of sensors driver and register them all.
125− */
126− int ret, i, id, sensor_num;
127− struct mfd_cell *sensor_cells;
128− struct cros_ec_sensor_platform *sensor_platforms;
129− int sensor_type[MOTIONSENSE_TYPE_MAX];
130− struct ec_params_motion_sense *params;
131− struct ec_response_motion_sense *resp;
132− struct cros_ec_command *msg;
133−
134− msg = kzalloc(sizeof(struct cros_ec_command) +
135− max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
136− if (msg == NULL)
137− return;
138−
139− msg->version = 2;
140− msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
141− msg->outsize = sizeof(*params);
142− msg->insize = sizeof(*resp);
143−
144− params = (struct ec_params_motion_sense *)msg->data;
145− params->cmd = MOTIONSENSE_CMD_DUMP;
146−
147− ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
148− if (ret < 0) {
149− dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
150− ret, msg->result);
151− goto error;
152− }
153−
154− resp = (struct ec_response_motion_sense *)msg->data;
155− sensor_num = resp->dump.sensor_count;
156− /*
157− * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
158− */
159− sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
160− GFP_KERNEL);
161− if (sensor_cells == NULL)
162− goto error;
163−
164− sensor_platforms = kcalloc(sensor_num,
165− sizeof(struct cros_ec_sensor_platform),
166− GFP_KERNEL);
167− if (sensor_platforms == NULL)
168− goto error_platforms;
169−
170− memset(sensor_type, 0, sizeof(sensor_type));
171− id = 0;
172− for (i = 0; i < sensor_num; i++) {
173− params->cmd = MOTIONSENSE_CMD_INFO;
174− params->info.sensor_num = i;
175− ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
176− if (ret < 0) {
177− dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
178− i, ret, msg->result);
179− continue;
180− }
181− switch (resp->info.type) {
182− case MOTIONSENSE_TYPE_ACCEL:
183− sensor_cells[id].name = "cros-ec-accel";
184− break;
185− case MOTIONSENSE_TYPE_BARO:
186− sensor_cells[id].name = "cros-ec-baro";
187− break;
188− case MOTIONSENSE_TYPE_GYRO:
189− sensor_cells[id].name = "cros-ec-gyro";
190− break;
191− case MOTIONSENSE_TYPE_MAG:
192− sensor_cells[id].name = "cros-ec-mag";
193− break;
194− case MOTIONSENSE_TYPE_PROX:
195− sensor_cells[id].name = "cros-ec-prox";
196− break;
197− case MOTIONSENSE_TYPE_LIGHT:
198− sensor_cells[id].name = "cros-ec-light";
199− break;
200− case MOTIONSENSE_TYPE_ACTIVITY:
201− sensor_cells[id].name = "cros-ec-activity";
202− break;
203− default:
204− dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
205− continue;
206− }
207− sensor_platforms[id].sensor_num = i;
208− sensor_cells[id].id = sensor_type[resp->info.type];
209− sensor_cells[id].platform_data = &sensor_platforms[id];
210− sensor_cells[id].pdata_size =
211− sizeof(struct cros_ec_sensor_platform);
212−
213− sensor_type[resp->info.type]++;
214− id++;
215− }
216−
217− if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
218− ec->has_kb_wake_angle = true;
219−
220− if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
221− sensor_cells[id].name = "cros-ec-ring";
222− id++;
223− }
224− if (cros_ec_check_features(ec,
225− EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
226− sensor_cells[id].name = "cros-ec-lid-angle";
227− id++;
228− }
229−
230− ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
231− NULL, 0, NULL);
232− if (ret)
233− dev_err(ec->dev, "failed to add EC sensors\n");
234−
235− kfree(sensor_platforms);
236−error_platforms:
237− kfree(sensor_cells);
238−error:
239− kfree(msg);
240−}
241−
242−static struct cros_ec_sensor_platform sensor_platforms[] = {
243− { .sensor_num = 0 },
244− { .sensor_num = 1 }
245−};
246−
247−static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
248− {
249− .name = "cros-ec-accel-legacy",
250− .platform_data = &sensor_platforms[0],
251− .pdata_size = sizeof(struct cros_ec_sensor_platform),
252− },
253− {
254− .name = "cros-ec-accel-legacy",
255− .platform_data = &sensor_platforms[1],
256− .pdata_size = sizeof(struct cros_ec_sensor_platform),
257− }
258−};
259−
260−static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
261−{
262− struct cros_ec_device *ec_dev = ec->ec_dev;
263− u8 status;
264− int ret;
265−
266− /*
267− * ECs that need legacy support are the main EC, directly connected to
268− * the AP.
269− */
270− if (ec->cmd_offset != 0)
271− return;
272−
273− /*
274− * Check if EC supports direct memory reads and if EC has
275− * accelerometers.
276− */
277− if (ec_dev->cmd_readmem) {
278− ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
279− &status);
280− if (ret < 0) {
281− dev_warn(ec->dev, "EC direct read error.\n");
282− return;
283− }
284−
285− /* Check if EC has accelerometers. */
286− if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
287− dev_info(ec->dev, "EC does not have accelerometers.\n");
288− return;
289− }
290− }
291−
292− /*
293− * The device may still support accelerometers:
294− * it would be an older ARM based device that do not suppor the
295− * EC_CMD_GET_FEATURES command.
296− *
297− * Register 2 accelerometers, we will fail in the IIO driver if there
298− * are no sensors.
299− */
300− ret = mfd_add_hotplug_devices(ec->dev, cros_ec_accel_legacy_cells,
301− ARRAY_SIZE(cros_ec_accel_legacy_cells));
302− if (ret)
303− dev_err(ec_dev->dev, "failed to add EC sensors\n");
304−}
305−
306124 static int ec_device_probe(struct platform_device *pdev)
307125 {
308126 int retval = -ENOMEM;
@@ -358,11 +176,14 @@ static int ec_device_probe(struct platform_device *pdev) · 49 unmodified lines
358176 goto failed;
359177
360178 /* check whether this EC is a sensor hub. */
361− if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
362− cros_ec_sensors_register(ec);
363− else
364− /* Workaroud for older EC firmware */
365− cros_ec_accel_legacy_register(ec);
179+ if (cros_ec_get_sensor_count(ec) > 0) {
180+ retval = mfd_add_hotplug_devices(ec->dev,
181+ cros_ec_sensorhub_cells,
182+ ARRAY_SIZE(cros_ec_sensorhub_cells));
183+ if (retval)
184+ dev_err(ec->dev, "failed to add %s subdevice: %d\n",
185+ cros_ec_sensorhub_cells->name, retval);
186+ }
366187
367188 /*
368189 * The following subdevices can be detected by sending the
include/linux/platform_data/cros_ec_proto.h+0−8
@@ -168,14 +168,6 @@ struct cros_ec_device { · 167 unmodified lines
168168 struct platform_device *pd;
169169 };
170170
171−/**
172− * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
173− * @sensor_num: Id of the sensor, as reported by the EC.
174− */
175−struct cros_ec_sensor_platform {
176− u8 sensor_num;
177−};
178−
179171 /**
180172 * struct cros_ec_platform - ChromeOS EC platform information.
181173 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
include/linux/platform_data/cros_ec_sensorhub.h+8−0
@@ -10,6 +10,14 @@ · 9 unmodified lines
1010
1111 #include <linux/platform_data/cros_ec_commands.h>
1212
13+/**
14+ * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
15+ * @sensor_num: Id of the sensor, as reported by the EC.
16+ */
17+struct cros_ec_sensor_platform {
18+ u8 sensor_num;
19+};
20+
1321 /**
1422 * struct cros_ec_sensorhub - Sensor Hub device data.
1523 *

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t