Blame view
drivers/mfd/max17135-core.c
6.1 KB
eeceaa1c7 MLK-11556-1 pmic:... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. * Copyright 2019 NXP */ /*! * @file pmic/core/max17135.c * @brief This file contains MAX17135 specific PMIC code. This implementaion * may differ for each PMIC chip. * * @ingroup PMIC_CORE */ /* * Includes */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/device.h> #include <linux/i2c.h> #include <linux/delay.h> #include <linux/err.h> #include <linux/uaccess.h> #include <linux/of.h> #include <linux/of_device.h> #include <linux/of_gpio.h> #include <linux/platform_device.h> #include <linux/regulator/machine.h> #include <linux/pmic_status.h> #include <linux/mfd/core.h> #include <linux/mfd/max17135.h> #include <asm/mach-types.h> static int max17135_detect(struct i2c_client *client, struct i2c_board_info *info); struct i2c_client *max17135_client; static struct regulator *gpio_regulator; static struct mfd_cell max17135_devs[] = { { .name = "max17135-pmic", }, { .name = "max17135-sns", }, }; static const unsigned short normal_i2c[] = {0x48, I2C_CLIENT_END}; int max17135_reg_read(int reg_num, unsigned int *reg_val) { int result; if (max17135_client == NULL) return PMIC_ERROR; if ((reg_num == REG_MAX17135_EXT_TEMP) || (reg_num == REG_MAX17135_INT_TEMP)) { result = i2c_smbus_read_word_data(max17135_client, reg_num); if (result < 0) { dev_err(&max17135_client->dev, "Unable to read MAX17135 register via I2C "); return PMIC_ERROR; } /* Swap bytes for dword read */ result = (result >> 8) | ((result & 0xFF) << 8); } else { result = i2c_smbus_read_byte_data(max17135_client, reg_num); if (result < 0) { dev_err(&max17135_client->dev, "Unable to read MAX17135 register via I2C "); return PMIC_ERROR; } } *reg_val = result; return PMIC_SUCCESS; } |
9202b784d MLK-13470 mfd: ma... |
80 |
EXPORT_SYMBOL(max17135_reg_read); |
eeceaa1c7 MLK-11556-1 pmic:... |
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
int max17135_reg_write(int reg_num, const unsigned int reg_val) { int result; if (max17135_client == NULL) return PMIC_ERROR; result = i2c_smbus_write_byte_data(max17135_client, reg_num, reg_val); if (result < 0) { dev_err(&max17135_client->dev, "Unable to write MAX17135 register via I2C "); return PMIC_ERROR; } return PMIC_SUCCESS; } |
9202b784d MLK-13470 mfd: ma... |
99 |
EXPORT_SYMBOL(max17135_reg_write); |
eeceaa1c7 MLK-11556-1 pmic:... |
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#ifdef CONFIG_OF static struct max17135_platform_data *max17135_i2c_parse_dt_pdata( struct device *dev) { struct max17135_platform_data *pdata; pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) { dev_err(dev, "could not allocate memory for pdata "); return ERR_PTR(-ENOMEM); } return pdata; } #else static struct max17135_platform_data *max17135_i2c_parse_dt_pdata( struct device *dev) { return NULL; } #endif /* !CONFIG_OF */ static int max17135_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct max17135 *max17135; struct max17135_platform_data *pdata = client->dev.platform_data; struct device_node *np = client->dev.of_node; int ret = 0; if (!np) return -ENODEV; gpio_regulator = devm_regulator_get(&client->dev, "SENSOR"); if (!IS_ERR(gpio_regulator)) { ret = regulator_enable(gpio_regulator); if (ret) { dev_err(&client->dev, "gpio set voltage error "); return ret; } } /* Create the PMIC data structure */ max17135 = kzalloc(sizeof(struct max17135), GFP_KERNEL); |
bfadaf739 MLK-11908 mfd: MA... |
147 |
if (max17135 == NULL) |
eeceaa1c7 MLK-11556-1 pmic:... |
148 |
return -ENOMEM; |
eeceaa1c7 MLK-11556-1 pmic:... |
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
/* Initialize the PMIC data structure */ i2c_set_clientdata(client, max17135); max17135->dev = &client->dev; max17135->i2c_client = client; max17135_client = client; ret = max17135_detect(client, NULL); if (ret) goto err1; mfd_add_devices(max17135->dev, -1, max17135_devs, ARRAY_SIZE(max17135_devs), NULL, 0, NULL); if (max17135->dev->of_node) { pdata = max17135_i2c_parse_dt_pdata(max17135->dev); if (IS_ERR(pdata)) { ret = PTR_ERR(pdata); goto err2; } } max17135->pdata = pdata; dev_info(&client->dev, "PMIC MAX17135 for eInk display "); return ret; err2: mfd_remove_devices(max17135->dev); err1: |
c94339309 mfd: max17135: fi... |
181 182 |
if (!IS_ERR(gpio_regulator)) regulator_disable(gpio_regulator); |
eeceaa1c7 MLK-11556-1 pmic:... |
183 184 185 186 187 188 189 190 191 192 193 |
kfree(max17135); return ret; } static int max17135_remove(struct i2c_client *i2c) { struct max17135 *max17135 = i2c_get_clientdata(i2c); mfd_remove_devices(max17135->dev); |
c94339309 mfd: max17135: fi... |
194 195 196 |
if (!IS_ERR(gpio_regulator)) regulator_disable(gpio_regulator); |
eeceaa1c7 MLK-11556-1 pmic:... |
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
return 0; } /* Return 0 if detection is successful, -ENODEV otherwise */ static int max17135_detect(struct i2c_client *client, struct i2c_board_info *info) { struct i2c_adapter *adapter = client->adapter; u8 chip_rev, chip_id; if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) return -ENODEV; /* detection */ if (i2c_smbus_read_byte_data(client, REG_MAX17135_PRODUCT_REV) != 0) { dev_err(&adapter->dev, "Max17135 PMIC not found! "); return -ENODEV; } /* identification */ chip_rev = i2c_smbus_read_byte_data(client, REG_MAX17135_PRODUCT_REV); chip_id = i2c_smbus_read_byte_data(client, REG_MAX17135_PRODUCT_ID); if (chip_rev != 0x00 || chip_id != 0x4D) { /* identification failed */ dev_info(&adapter->dev, "Unsupported chip (man_id=0x%02X, " "chip_id=0x%02X). ", chip_rev, chip_id); return -ENODEV; } if (info) strlcpy(info->type, "max17135_sensor", I2C_NAME_SIZE); return 0; } static const struct i2c_device_id max17135_id[] = { { "max17135", 0 }, { } }; MODULE_DEVICE_TABLE(i2c, max17135_id); static const struct of_device_id max17135_dt_ids[] = { { .compatible = "maxim,max17135", .data = (void *) &max17135_id[0], }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, max17135_dt_ids); static struct i2c_driver max17135_driver = { .driver = { .name = "max17135", .owner = THIS_MODULE, .of_match_table = max17135_dt_ids, }, .probe = max17135_probe, .remove = max17135_remove, .id_table = max17135_id, .detect = max17135_detect, .address_list = &normal_i2c[0], }; static int __init max17135_init(void) { return i2c_add_driver(&max17135_driver); } static void __exit max17135_exit(void) { i2c_del_driver(&max17135_driver); } /* * Module entry points */ subsys_initcall(max17135_init); module_exit(max17135_exit); |