| .. | .. |
|---|
| 31 | 31 | #include <linux/of_device.h> |
|---|
| 32 | 32 | #include <linux/of_irq.h> |
|---|
| 33 | 33 | #include <linux/pm_runtime.h> |
|---|
| 34 | +#include <linux/regulator/consumer.h> |
|---|
| 34 | 35 | |
|---|
| 35 | 36 | #define MMA8452_STATUS 0x00 |
|---|
| 36 | 37 | #define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0)) |
|---|
| .. | .. |
|---|
| 107 | 108 | u8 data_cfg; |
|---|
| 108 | 109 | const struct mma_chip_info *chip_info; |
|---|
| 109 | 110 | int sleep_val; |
|---|
| 111 | + struct regulator *vdd_reg; |
|---|
| 112 | + struct regulator *vddio_reg; |
|---|
| 110 | 113 | |
|---|
| 111 | 114 | /* Ensure correct alignment of time stamp when present */ |
|---|
| 112 | 115 | struct { |
|---|
| .. | .. |
|---|
| 173 | 176 | * @enabled_events: event flags enabled and handled by this driver |
|---|
| 174 | 177 | */ |
|---|
| 175 | 178 | struct mma_chip_info { |
|---|
| 179 | + const char *name; |
|---|
| 176 | 180 | u8 chip_id; |
|---|
| 177 | 181 | const struct iio_chan_spec *channels; |
|---|
| 178 | 182 | int num_channels; |
|---|
| .. | .. |
|---|
| 1300 | 1304 | |
|---|
| 1301 | 1305 | static const struct mma_chip_info mma_chip_info_table[] = { |
|---|
| 1302 | 1306 | [mma8451] = { |
|---|
| 1307 | + .name = "mma8451", |
|---|
| 1303 | 1308 | .chip_id = MMA8451_DEVICE_ID, |
|---|
| 1304 | 1309 | .channels = mma8451_channels, |
|---|
| 1305 | 1310 | .num_channels = ARRAY_SIZE(mma8451_channels), |
|---|
| .. | .. |
|---|
| 1324 | 1329 | MMA8452_INT_FF_MT, |
|---|
| 1325 | 1330 | }, |
|---|
| 1326 | 1331 | [mma8452] = { |
|---|
| 1332 | + .name = "mma8452", |
|---|
| 1327 | 1333 | .chip_id = MMA8452_DEVICE_ID, |
|---|
| 1328 | 1334 | .channels = mma8452_channels, |
|---|
| 1329 | 1335 | .num_channels = ARRAY_SIZE(mma8452_channels), |
|---|
| .. | .. |
|---|
| 1340 | 1346 | MMA8452_INT_FF_MT, |
|---|
| 1341 | 1347 | }, |
|---|
| 1342 | 1348 | [mma8453] = { |
|---|
| 1349 | + .name = "mma8453", |
|---|
| 1343 | 1350 | .chip_id = MMA8453_DEVICE_ID, |
|---|
| 1344 | 1351 | .channels = mma8453_channels, |
|---|
| 1345 | 1352 | .num_channels = ARRAY_SIZE(mma8453_channels), |
|---|
| .. | .. |
|---|
| 1356 | 1363 | MMA8452_INT_FF_MT, |
|---|
| 1357 | 1364 | }, |
|---|
| 1358 | 1365 | [mma8652] = { |
|---|
| 1366 | + .name = "mma8652", |
|---|
| 1359 | 1367 | .chip_id = MMA8652_DEVICE_ID, |
|---|
| 1360 | 1368 | .channels = mma8652_channels, |
|---|
| 1361 | 1369 | .num_channels = ARRAY_SIZE(mma8652_channels), |
|---|
| .. | .. |
|---|
| 1365 | 1373 | .enabled_events = MMA8452_INT_FF_MT, |
|---|
| 1366 | 1374 | }, |
|---|
| 1367 | 1375 | [mma8653] = { |
|---|
| 1376 | + .name = "mma8653", |
|---|
| 1368 | 1377 | .chip_id = MMA8653_DEVICE_ID, |
|---|
| 1369 | 1378 | .channels = mma8653_channels, |
|---|
| 1370 | 1379 | .num_channels = ARRAY_SIZE(mma8653_channels), |
|---|
| .. | .. |
|---|
| 1379 | 1388 | .enabled_events = MMA8452_INT_FF_MT, |
|---|
| 1380 | 1389 | }, |
|---|
| 1381 | 1390 | [fxls8471] = { |
|---|
| 1391 | + .name = "fxls8471", |
|---|
| 1382 | 1392 | .chip_id = FXLS8471_DEVICE_ID, |
|---|
| 1383 | 1393 | .channels = mma8451_channels, |
|---|
| 1384 | 1394 | .num_channels = ARRAY_SIZE(mma8451_channels), |
|---|
| .. | .. |
|---|
| 1486 | 1496 | int i; |
|---|
| 1487 | 1497 | int ret; |
|---|
| 1488 | 1498 | |
|---|
| 1489 | | - ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2, |
|---|
| 1499 | + /* |
|---|
| 1500 | + * Find on fxls8471, after config reset bit, it reset immediately, |
|---|
| 1501 | + * and will not give ACK, so here do not check the return value. |
|---|
| 1502 | + * The following code will read the reset register, and check whether |
|---|
| 1503 | + * this reset works. |
|---|
| 1504 | + */ |
|---|
| 1505 | + i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2, |
|---|
| 1490 | 1506 | MMA8452_CTRL_REG2_RST); |
|---|
| 1491 | | - if (ret < 0) |
|---|
| 1492 | | - return ret; |
|---|
| 1493 | 1507 | |
|---|
| 1494 | 1508 | for (i = 0; i < 10; i++) { |
|---|
| 1495 | 1509 | usleep_range(100, 200); |
|---|
| .. | .. |
|---|
| 1522 | 1536 | struct mma8452_data *data; |
|---|
| 1523 | 1537 | struct iio_dev *indio_dev; |
|---|
| 1524 | 1538 | int ret; |
|---|
| 1525 | | - const struct of_device_id *match; |
|---|
| 1526 | | - |
|---|
| 1527 | | - match = of_match_device(mma8452_dt_ids, &client->dev); |
|---|
| 1528 | | - if (!match) { |
|---|
| 1529 | | - dev_err(&client->dev, "unknown device model\n"); |
|---|
| 1530 | | - return -ENODEV; |
|---|
| 1531 | | - } |
|---|
| 1532 | 1539 | |
|---|
| 1533 | 1540 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
|---|
| 1534 | 1541 | if (!indio_dev) |
|---|
| .. | .. |
|---|
| 1537 | 1544 | data = iio_priv(indio_dev); |
|---|
| 1538 | 1545 | data->client = client; |
|---|
| 1539 | 1546 | mutex_init(&data->lock); |
|---|
| 1540 | | - data->chip_info = match->data; |
|---|
| 1547 | + |
|---|
| 1548 | + data->chip_info = device_get_match_data(&client->dev); |
|---|
| 1549 | + if (!data->chip_info) { |
|---|
| 1550 | + if (id) { |
|---|
| 1551 | + data->chip_info = &mma_chip_info_table[id->driver_data]; |
|---|
| 1552 | + } else { |
|---|
| 1553 | + dev_err(&client->dev, "unknown device model\n"); |
|---|
| 1554 | + return -ENODEV; |
|---|
| 1555 | + } |
|---|
| 1556 | + } |
|---|
| 1557 | + |
|---|
| 1558 | + data->vdd_reg = devm_regulator_get(&client->dev, "vdd"); |
|---|
| 1559 | + if (IS_ERR(data->vdd_reg)) |
|---|
| 1560 | + return dev_err_probe(&client->dev, PTR_ERR(data->vdd_reg), |
|---|
| 1561 | + "failed to get VDD regulator!\n"); |
|---|
| 1562 | + |
|---|
| 1563 | + data->vddio_reg = devm_regulator_get(&client->dev, "vddio"); |
|---|
| 1564 | + if (IS_ERR(data->vddio_reg)) |
|---|
| 1565 | + return dev_err_probe(&client->dev, PTR_ERR(data->vddio_reg), |
|---|
| 1566 | + "failed to get VDDIO regulator!\n"); |
|---|
| 1567 | + |
|---|
| 1568 | + ret = regulator_enable(data->vdd_reg); |
|---|
| 1569 | + if (ret) { |
|---|
| 1570 | + dev_err(&client->dev, "failed to enable VDD regulator!\n"); |
|---|
| 1571 | + return ret; |
|---|
| 1572 | + } |
|---|
| 1573 | + |
|---|
| 1574 | + ret = regulator_enable(data->vddio_reg); |
|---|
| 1575 | + if (ret) { |
|---|
| 1576 | + dev_err(&client->dev, "failed to enable VDDIO regulator!\n"); |
|---|
| 1577 | + goto disable_regulator_vdd; |
|---|
| 1578 | + } |
|---|
| 1541 | 1579 | |
|---|
| 1542 | 1580 | ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I); |
|---|
| 1543 | 1581 | if (ret < 0) |
|---|
| 1544 | | - return ret; |
|---|
| 1582 | + goto disable_regulators; |
|---|
| 1545 | 1583 | |
|---|
| 1546 | 1584 | switch (ret) { |
|---|
| 1547 | 1585 | case MMA8451_DEVICE_ID: |
|---|
| .. | .. |
|---|
| 1552 | 1590 | case FXLS8471_DEVICE_ID: |
|---|
| 1553 | 1591 | if (ret == data->chip_info->chip_id) |
|---|
| 1554 | 1592 | break; |
|---|
| 1555 | | - /* else: fall through */ |
|---|
| 1593 | + fallthrough; |
|---|
| 1556 | 1594 | default: |
|---|
| 1557 | | - return -ENODEV; |
|---|
| 1595 | + ret = -ENODEV; |
|---|
| 1596 | + goto disable_regulators; |
|---|
| 1558 | 1597 | } |
|---|
| 1559 | 1598 | |
|---|
| 1560 | 1599 | dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n", |
|---|
| 1561 | | - match->compatible, data->chip_info->chip_id); |
|---|
| 1600 | + data->chip_info->name, data->chip_info->chip_id); |
|---|
| 1562 | 1601 | |
|---|
| 1563 | 1602 | i2c_set_clientdata(client, indio_dev); |
|---|
| 1564 | 1603 | indio_dev->info = &mma8452_info; |
|---|
| 1565 | | - indio_dev->name = id->name; |
|---|
| 1566 | | - indio_dev->dev.parent = &client->dev; |
|---|
| 1604 | + indio_dev->name = data->chip_info->name; |
|---|
| 1567 | 1605 | indio_dev->modes = INDIO_DIRECT_MODE; |
|---|
| 1568 | 1606 | indio_dev->channels = data->chip_info->channels; |
|---|
| 1569 | 1607 | indio_dev->num_channels = data->chip_info->num_channels; |
|---|
| .. | .. |
|---|
| 1571 | 1609 | |
|---|
| 1572 | 1610 | ret = mma8452_reset(client); |
|---|
| 1573 | 1611 | if (ret < 0) |
|---|
| 1574 | | - return ret; |
|---|
| 1612 | + goto disable_regulators; |
|---|
| 1575 | 1613 | |
|---|
| 1576 | 1614 | data->data_cfg = MMA8452_DATA_CFG_FS_2G; |
|---|
| 1577 | 1615 | ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG, |
|---|
| 1578 | 1616 | data->data_cfg); |
|---|
| 1579 | 1617 | if (ret < 0) |
|---|
| 1580 | | - return ret; |
|---|
| 1618 | + goto disable_regulators; |
|---|
| 1581 | 1619 | |
|---|
| 1582 | 1620 | /* |
|---|
| 1583 | 1621 | * By default set transient threshold to max to avoid events if |
|---|
| .. | .. |
|---|
| 1586 | 1624 | ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS, |
|---|
| 1587 | 1625 | MMA8452_TRANSIENT_THS_MASK); |
|---|
| 1588 | 1626 | if (ret < 0) |
|---|
| 1589 | | - return ret; |
|---|
| 1627 | + goto disable_regulators; |
|---|
| 1590 | 1628 | |
|---|
| 1591 | 1629 | if (client->irq) { |
|---|
| 1592 | 1630 | int irq2; |
|---|
| .. | .. |
|---|
| 1600 | 1638 | MMA8452_CTRL_REG5, |
|---|
| 1601 | 1639 | data->chip_info->all_events); |
|---|
| 1602 | 1640 | if (ret < 0) |
|---|
| 1603 | | - return ret; |
|---|
| 1641 | + goto disable_regulators; |
|---|
| 1604 | 1642 | |
|---|
| 1605 | 1643 | dev_dbg(&client->dev, "using interrupt line INT1\n"); |
|---|
| 1606 | 1644 | } |
|---|
| .. | .. |
|---|
| 1609 | 1647 | MMA8452_CTRL_REG4, |
|---|
| 1610 | 1648 | data->chip_info->enabled_events); |
|---|
| 1611 | 1649 | if (ret < 0) |
|---|
| 1612 | | - return ret; |
|---|
| 1650 | + goto disable_regulators; |
|---|
| 1613 | 1651 | |
|---|
| 1614 | 1652 | ret = mma8452_trigger_setup(indio_dev); |
|---|
| 1615 | 1653 | if (ret < 0) |
|---|
| 1616 | | - return ret; |
|---|
| 1654 | + goto disable_regulators; |
|---|
| 1617 | 1655 | } |
|---|
| 1618 | 1656 | |
|---|
| 1619 | 1657 | data->ctrl_reg1 = MMA8452_CTRL_ACTIVE | |
|---|
| .. | .. |
|---|
| 1669 | 1707 | trigger_cleanup: |
|---|
| 1670 | 1708 | mma8452_trigger_cleanup(indio_dev); |
|---|
| 1671 | 1709 | |
|---|
| 1710 | +disable_regulators: |
|---|
| 1711 | + regulator_disable(data->vddio_reg); |
|---|
| 1712 | + |
|---|
| 1713 | +disable_regulator_vdd: |
|---|
| 1714 | + regulator_disable(data->vdd_reg); |
|---|
| 1715 | + |
|---|
| 1672 | 1716 | return ret; |
|---|
| 1673 | 1717 | } |
|---|
| 1674 | 1718 | |
|---|
| 1675 | 1719 | static int mma8452_remove(struct i2c_client *client) |
|---|
| 1676 | 1720 | { |
|---|
| 1677 | 1721 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
|---|
| 1722 | + struct mma8452_data *data = iio_priv(indio_dev); |
|---|
| 1678 | 1723 | |
|---|
| 1679 | 1724 | iio_device_unregister(indio_dev); |
|---|
| 1680 | 1725 | |
|---|
| .. | .. |
|---|
| 1685 | 1730 | iio_triggered_buffer_cleanup(indio_dev); |
|---|
| 1686 | 1731 | mma8452_trigger_cleanup(indio_dev); |
|---|
| 1687 | 1732 | mma8452_standby(iio_priv(indio_dev)); |
|---|
| 1733 | + |
|---|
| 1734 | + regulator_disable(data->vddio_reg); |
|---|
| 1735 | + regulator_disable(data->vdd_reg); |
|---|
| 1688 | 1736 | |
|---|
| 1689 | 1737 | return 0; |
|---|
| 1690 | 1738 | } |
|---|
| .. | .. |
|---|
| 1704 | 1752 | return -EAGAIN; |
|---|
| 1705 | 1753 | } |
|---|
| 1706 | 1754 | |
|---|
| 1755 | + ret = regulator_disable(data->vddio_reg); |
|---|
| 1756 | + if (ret) { |
|---|
| 1757 | + dev_err(dev, "failed to disable VDDIO regulator\n"); |
|---|
| 1758 | + return ret; |
|---|
| 1759 | + } |
|---|
| 1760 | + |
|---|
| 1761 | + ret = regulator_disable(data->vdd_reg); |
|---|
| 1762 | + if (ret) { |
|---|
| 1763 | + dev_err(dev, "failed to disable VDD regulator\n"); |
|---|
| 1764 | + return ret; |
|---|
| 1765 | + } |
|---|
| 1766 | + |
|---|
| 1707 | 1767 | return 0; |
|---|
| 1708 | 1768 | } |
|---|
| 1709 | 1769 | |
|---|
| .. | .. |
|---|
| 1713 | 1773 | struct mma8452_data *data = iio_priv(indio_dev); |
|---|
| 1714 | 1774 | int ret, sleep_val; |
|---|
| 1715 | 1775 | |
|---|
| 1776 | + ret = regulator_enable(data->vdd_reg); |
|---|
| 1777 | + if (ret) { |
|---|
| 1778 | + dev_err(dev, "failed to enable VDD regulator\n"); |
|---|
| 1779 | + return ret; |
|---|
| 1780 | + } |
|---|
| 1781 | + |
|---|
| 1782 | + ret = regulator_enable(data->vddio_reg); |
|---|
| 1783 | + if (ret) { |
|---|
| 1784 | + dev_err(dev, "failed to enable VDDIO regulator\n"); |
|---|
| 1785 | + regulator_disable(data->vdd_reg); |
|---|
| 1786 | + return ret; |
|---|
| 1787 | + } |
|---|
| 1788 | + |
|---|
| 1716 | 1789 | ret = mma8452_active(data); |
|---|
| 1717 | 1790 | if (ret < 0) |
|---|
| 1718 | | - return ret; |
|---|
| 1791 | + goto runtime_resume_failed; |
|---|
| 1719 | 1792 | |
|---|
| 1720 | 1793 | ret = mma8452_get_odr_index(data); |
|---|
| 1721 | 1794 | sleep_val = 1000 / mma8452_samp_freq[ret][0]; |
|---|
| .. | .. |
|---|
| 1725 | 1798 | msleep_interruptible(sleep_val); |
|---|
| 1726 | 1799 | |
|---|
| 1727 | 1800 | return 0; |
|---|
| 1728 | | -} |
|---|
| 1729 | | -#endif |
|---|
| 1730 | 1801 | |
|---|
| 1731 | | -#ifdef CONFIG_PM_SLEEP |
|---|
| 1732 | | -static int mma8452_suspend(struct device *dev) |
|---|
| 1733 | | -{ |
|---|
| 1734 | | - return mma8452_standby(iio_priv(i2c_get_clientdata( |
|---|
| 1735 | | - to_i2c_client(dev)))); |
|---|
| 1736 | | -} |
|---|
| 1802 | +runtime_resume_failed: |
|---|
| 1803 | + regulator_disable(data->vddio_reg); |
|---|
| 1804 | + regulator_disable(data->vdd_reg); |
|---|
| 1737 | 1805 | |
|---|
| 1738 | | -static int mma8452_resume(struct device *dev) |
|---|
| 1739 | | -{ |
|---|
| 1740 | | - return mma8452_active(iio_priv(i2c_get_clientdata( |
|---|
| 1741 | | - to_i2c_client(dev)))); |
|---|
| 1806 | + return ret; |
|---|
| 1742 | 1807 | } |
|---|
| 1743 | 1808 | #endif |
|---|
| 1744 | 1809 | |
|---|
| 1745 | 1810 | static const struct dev_pm_ops mma8452_pm_ops = { |
|---|
| 1746 | | - SET_SYSTEM_SLEEP_PM_OPS(mma8452_suspend, mma8452_resume) |
|---|
| 1811 | + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) |
|---|
| 1747 | 1812 | SET_RUNTIME_PM_OPS(mma8452_runtime_suspend, |
|---|
| 1748 | 1813 | mma8452_runtime_resume, NULL) |
|---|
| 1749 | 1814 | }; |
|---|
| .. | .. |
|---|
| 1762 | 1827 | static struct i2c_driver mma8452_driver = { |
|---|
| 1763 | 1828 | .driver = { |
|---|
| 1764 | 1829 | .name = "mma8452", |
|---|
| 1765 | | - .of_match_table = of_match_ptr(mma8452_dt_ids), |
|---|
| 1830 | + .of_match_table = mma8452_dt_ids, |
|---|
| 1766 | 1831 | .pm = &mma8452_pm_ops, |
|---|
| 1767 | 1832 | }, |
|---|
| 1768 | 1833 | .probe = mma8452_probe, |
|---|