Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * I2C access for DA9052 PMICs.
0004  *
0005  * Copyright(c) 2011 Dialog Semiconductor Ltd.
0006  *
0007  * Author: David Dajun Chen <dchen@diasemi.com>
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/module.h>
0012 #include <linux/input.h>
0013 #include <linux/mfd/core.h>
0014 #include <linux/i2c.h>
0015 #include <linux/err.h>
0016 
0017 #include <linux/mfd/da9052/da9052.h>
0018 #include <linux/mfd/da9052/reg.h>
0019 
0020 #ifdef CONFIG_OF
0021 #include <linux/of.h>
0022 #include <linux/of_device.h>
0023 #endif
0024 
0025 /* I2C safe register check */
0026 static inline bool i2c_safe_reg(unsigned char reg)
0027 {
0028     switch (reg) {
0029     case DA9052_STATUS_A_REG:
0030     case DA9052_STATUS_B_REG:
0031     case DA9052_STATUS_C_REG:
0032     case DA9052_STATUS_D_REG:
0033     case DA9052_ADC_RES_L_REG:
0034     case DA9052_ADC_RES_H_REG:
0035     case DA9052_VDD_RES_REG:
0036     case DA9052_ICHG_AV_REG:
0037     case DA9052_TBAT_RES_REG:
0038     case DA9052_ADCIN4_RES_REG:
0039     case DA9052_ADCIN5_RES_REG:
0040     case DA9052_ADCIN6_RES_REG:
0041     case DA9052_TJUNC_RES_REG:
0042     case DA9052_TSI_X_MSB_REG:
0043     case DA9052_TSI_Y_MSB_REG:
0044     case DA9052_TSI_LSB_REG:
0045     case DA9052_TSI_Z_MSB_REG:
0046         return true;
0047     default:
0048         return false;
0049     }
0050 }
0051 
0052 /*
0053  * There is an issue with DA9052 and DA9053_AA/BA/BB PMIC where the PMIC
0054  * gets lockup up or fails to respond following a system reset.
0055  * This fix is to follow any read or write with a dummy read to a safe
0056  * register.
0057  */
0058 static int da9052_i2c_fix(struct da9052 *da9052, unsigned char reg)
0059 {
0060     int val;
0061 
0062     switch (da9052->chip_id) {
0063     case DA9052:
0064     case DA9053_AA:
0065     case DA9053_BA:
0066     case DA9053_BB:
0067         /* A dummy read to a safe register address. */
0068         if (!i2c_safe_reg(reg))
0069             return regmap_read(da9052->regmap,
0070                        DA9052_PARK_REGISTER,
0071                        &val);
0072         break;
0073     case DA9053_BC:
0074     default:
0075         /*
0076          * For other chips parking of I2C register
0077          * to a safe place is not required.
0078          */
0079         break;
0080     }
0081 
0082     return 0;
0083 }
0084 
0085 /*
0086  * According to errata item 24, multiwrite mode should be avoided
0087  * in order to prevent register data corruption after power-down.
0088  */
0089 static int da9052_i2c_disable_multiwrite(struct da9052 *da9052)
0090 {
0091     int reg_val, ret;
0092 
0093     ret = regmap_read(da9052->regmap, DA9052_CONTROL_B_REG, &reg_val);
0094     if (ret < 0)
0095         return ret;
0096 
0097     if (!(reg_val & DA9052_CONTROL_B_WRITEMODE)) {
0098         reg_val |= DA9052_CONTROL_B_WRITEMODE;
0099         ret = regmap_write(da9052->regmap, DA9052_CONTROL_B_REG,
0100                    reg_val);
0101         if (ret < 0)
0102             return ret;
0103     }
0104 
0105     return 0;
0106 }
0107 
0108 static const struct i2c_device_id da9052_i2c_id[] = {
0109     {"da9052", DA9052},
0110     {"da9053-aa", DA9053_AA},
0111     {"da9053-ba", DA9053_BA},
0112     {"da9053-bb", DA9053_BB},
0113     {"da9053-bc", DA9053_BC},
0114     {}
0115 };
0116 MODULE_DEVICE_TABLE(i2c, da9052_i2c_id);
0117 
0118 #ifdef CONFIG_OF
0119 static const struct of_device_id dialog_dt_ids[] = {
0120     { .compatible = "dlg,da9052", .data = &da9052_i2c_id[0] },
0121     { .compatible = "dlg,da9053-aa", .data = &da9052_i2c_id[1] },
0122     { .compatible = "dlg,da9053-ba", .data = &da9052_i2c_id[2] },
0123     { .compatible = "dlg,da9053-bb", .data = &da9052_i2c_id[3] },
0124     { .compatible = "dlg,da9053-bc", .data = &da9052_i2c_id[4] },
0125     { /* sentinel */ }
0126 };
0127 #endif
0128 
0129 static int da9052_i2c_probe(struct i2c_client *client,
0130                        const struct i2c_device_id *id)
0131 {
0132     struct da9052 *da9052;
0133     int ret;
0134 
0135     da9052 = devm_kzalloc(&client->dev, sizeof(struct da9052), GFP_KERNEL);
0136     if (!da9052)
0137         return -ENOMEM;
0138 
0139     da9052->dev = &client->dev;
0140     da9052->chip_irq = client->irq;
0141     da9052->fix_io = da9052_i2c_fix;
0142 
0143     i2c_set_clientdata(client, da9052);
0144 
0145     da9052->regmap = devm_regmap_init_i2c(client, &da9052_regmap_config);
0146     if (IS_ERR(da9052->regmap)) {
0147         ret = PTR_ERR(da9052->regmap);
0148         dev_err(&client->dev, "Failed to allocate register map: %d\n",
0149             ret);
0150         return ret;
0151     }
0152 
0153     ret = da9052_i2c_disable_multiwrite(da9052);
0154     if (ret < 0)
0155         return ret;
0156 
0157 #ifdef CONFIG_OF
0158     if (!id)
0159         id = of_device_get_match_data(&client->dev);
0160 #endif
0161 
0162     if (!id) {
0163         ret = -ENODEV;
0164         dev_err(&client->dev, "id is null.\n");
0165         return ret;
0166     }
0167 
0168     return da9052_device_init(da9052, id->driver_data);
0169 }
0170 
0171 static int da9052_i2c_remove(struct i2c_client *client)
0172 {
0173     struct da9052 *da9052 = i2c_get_clientdata(client);
0174 
0175     da9052_device_exit(da9052);
0176     return 0;
0177 }
0178 
0179 static struct i2c_driver da9052_i2c_driver = {
0180     .probe = da9052_i2c_probe,
0181     .remove = da9052_i2c_remove,
0182     .id_table = da9052_i2c_id,
0183     .driver = {
0184         .name = "da9052",
0185 #ifdef CONFIG_OF
0186         .of_match_table = dialog_dt_ids,
0187 #endif
0188     },
0189 };
0190 
0191 static int __init da9052_i2c_init(void)
0192 {
0193     int ret;
0194 
0195     ret = i2c_add_driver(&da9052_i2c_driver);
0196     if (ret != 0) {
0197         pr_err("DA9052 I2C registration failed %d\n", ret);
0198         return ret;
0199     }
0200 
0201     return 0;
0202 }
0203 subsys_initcall(da9052_i2c_init);
0204 
0205 static void __exit da9052_i2c_exit(void)
0206 {
0207     i2c_del_driver(&da9052_i2c_driver);
0208 }
0209 module_exit(da9052_i2c_exit);
0210 
0211 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
0212 MODULE_DESCRIPTION("I2C driver for Dialog DA9052 PMIC");
0213 MODULE_LICENSE("GPL");