^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * AS3711 PMIC MFC driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Renesas Electronics Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/as3711.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) AS3711_REGULATOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) AS3711_BACKLIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Ok to have it static: it is only used during probing and multiple I2C devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * cannot be probed simultaneously. Just make sure to avoid stale data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct mfd_cell as3711_subdevs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) [AS3711_REGULATOR] = {.name = "as3711-regulator",},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) [AS3711_BACKLIGHT] = {.name = "as3711-backlight",},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static bool as3711_volatile_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) case AS3711_GPIO_SIGNAL_IN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) case AS3711_INTERRUPT_STATUS_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) case AS3711_INTERRUPT_STATUS_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case AS3711_INTERRUPT_STATUS_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) case AS3711_CHARGER_STATUS_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) case AS3711_CHARGER_STATUS_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case AS3711_REG_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static bool as3711_precious_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) case AS3711_INTERRUPT_STATUS_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) case AS3711_INTERRUPT_STATUS_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) case AS3711_INTERRUPT_STATUS_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static bool as3711_readable_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) case AS3711_SD_1_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) case AS3711_SD_2_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case AS3711_SD_3_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) case AS3711_SD_4_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) case AS3711_LDO_1_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case AS3711_LDO_2_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) case AS3711_LDO_3_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) case AS3711_LDO_4_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case AS3711_LDO_5_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) case AS3711_LDO_6_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) case AS3711_LDO_7_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case AS3711_LDO_8_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case AS3711_SD_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) case AS3711_GPIO_SIGNAL_OUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case AS3711_GPIO_SIGNAL_IN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) case AS3711_SD_CONTROL_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) case AS3711_SD_CONTROL_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) case AS3711_CURR_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case AS3711_CURR1_VALUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) case AS3711_CURR2_VALUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) case AS3711_CURR3_VALUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case AS3711_STEPUP_CONTROL_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) case AS3711_STEPUP_CONTROL_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case AS3711_STEPUP_CONTROL_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case AS3711_STEPUP_CONTROL_5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) case AS3711_REG_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case AS3711_INTERRUPT_STATUS_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case AS3711_INTERRUPT_STATUS_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) case AS3711_INTERRUPT_STATUS_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case AS3711_CHARGER_STATUS_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case AS3711_CHARGER_STATUS_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) case AS3711_ASIC_ID_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) case AS3711_ASIC_ID_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static const struct regmap_config as3711_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .volatile_reg = as3711_volatile_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .readable_reg = as3711_readable_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .precious_reg = as3711_precious_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .max_register = AS3711_MAX_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .num_reg_defaults_raw = AS3711_NUM_REGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .cache_type = REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static const struct of_device_id as3711_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {.compatible = "ams,as3711",},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int as3711_i2c_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct as3711 *as3711;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct as3711_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned int id1, id2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!client->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pdata = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) dev_dbg(&client->dev, "Platform data not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pdata = devm_kzalloc(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) as3711 = devm_kzalloc(&client->dev, sizeof(struct as3711), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (!as3711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) as3711->dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) i2c_set_clientdata(client, as3711);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (client->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) dev_notice(&client->dev, "IRQ not supported yet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) as3711->regmap = devm_regmap_init_i2c(client, &as3711_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (IS_ERR(as3711->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = PTR_ERR(as3711->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) "regmap initialization failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_1, &id1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_2, &id2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) dev_err(&client->dev, "regmap_read() failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (id1 != 0x8b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_info(as3711->dev, "AS3711 detected: %x:%x\n", id1, id2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * We can reuse as3711_subdevs[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * it will be copied in mfd_add_devices()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) as3711_subdevs[AS3711_REGULATOR].platform_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) &pdata->regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) as3711_subdevs[AS3711_REGULATOR].pdata_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) sizeof(pdata->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) as3711_subdevs[AS3711_BACKLIGHT].platform_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) &pdata->backlight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) as3711_subdevs[AS3711_BACKLIGHT].pdata_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) sizeof(pdata->backlight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) as3711_subdevs[AS3711_REGULATOR].platform_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) as3711_subdevs[AS3711_REGULATOR].pdata_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) as3711_subdevs[AS3711_BACKLIGHT].platform_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) as3711_subdevs[AS3711_BACKLIGHT].pdata_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = devm_mfd_add_devices(as3711->dev, -1, as3711_subdevs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ARRAY_SIZE(as3711_subdevs), NULL, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dev_err(&client->dev, "add mfd devices failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static const struct i2c_device_id as3711_i2c_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {.name = "as3711", .driver_data = 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static struct i2c_driver as3711_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .name = "as3711",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .of_match_table = of_match_ptr(as3711_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .probe = as3711_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .id_table = as3711_i2c_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int __init as3711_i2c_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return i2c_add_driver(&as3711_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* Initialise early */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) subsys_initcall(as3711_i2c_init);