xref: /openbmc/linux/drivers/mfd/max77693.c (revision 4657185c)
183871c00SChanwoo Choi /*
283871c00SChanwoo Choi  * max77693.c - mfd core driver for the MAX 77693
383871c00SChanwoo Choi  *
483871c00SChanwoo Choi  * Copyright (C) 2012 Samsung Electronics
583871c00SChanwoo Choi  * SangYoung Son <hello.son@smasung.com>
683871c00SChanwoo Choi  *
783871c00SChanwoo Choi  * This program is not provided / owned by Maxim Integrated Products.
883871c00SChanwoo Choi  *
983871c00SChanwoo Choi  * This program is free software; you can redistribute it and/or modify
1083871c00SChanwoo Choi  * it under the terms of the GNU General Public License as published by
1183871c00SChanwoo Choi  * the Free Software Foundation; either version 2 of the License, or
1283871c00SChanwoo Choi  * (at your option) any later version.
1383871c00SChanwoo Choi  *
1483871c00SChanwoo Choi  * This program is distributed in the hope that it will be useful,
1583871c00SChanwoo Choi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1683871c00SChanwoo Choi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1783871c00SChanwoo Choi  * GNU General Public License for more details.
1883871c00SChanwoo Choi  *
1983871c00SChanwoo Choi  * You should have received a copy of the GNU General Public License
2083871c00SChanwoo Choi  * along with this program; if not, write to the Free Software
2183871c00SChanwoo Choi  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2283871c00SChanwoo Choi  *
2383871c00SChanwoo Choi  * This driver is based on max8997.c
2483871c00SChanwoo Choi  */
2583871c00SChanwoo Choi 
2683871c00SChanwoo Choi #include <linux/module.h>
2783871c00SChanwoo Choi #include <linux/slab.h>
2883871c00SChanwoo Choi #include <linux/i2c.h>
2983871c00SChanwoo Choi #include <linux/err.h>
3083871c00SChanwoo Choi #include <linux/interrupt.h>
3183871c00SChanwoo Choi #include <linux/pm_runtime.h>
3283871c00SChanwoo Choi #include <linux/mutex.h>
3383871c00SChanwoo Choi #include <linux/mfd/core.h>
3483871c00SChanwoo Choi #include <linux/mfd/max77693.h>
3583871c00SChanwoo Choi #include <linux/mfd/max77693-private.h>
3683871c00SChanwoo Choi #include <linux/regulator/machine.h>
3783871c00SChanwoo Choi #include <linux/regmap.h>
3883871c00SChanwoo Choi 
3983871c00SChanwoo Choi #define I2C_ADDR_PMIC	(0xCC >> 1)	/* Charger, Flash LED */
4083871c00SChanwoo Choi #define I2C_ADDR_MUIC	(0x4A >> 1)
4183871c00SChanwoo Choi #define I2C_ADDR_HAPTIC	(0x90 >> 1)
4283871c00SChanwoo Choi 
4383871c00SChanwoo Choi static struct mfd_cell max77693_devs[] = {
4483871c00SChanwoo Choi 	{ .name = "max77693-pmic", },
4583871c00SChanwoo Choi 	{ .name = "max77693-charger", },
4683871c00SChanwoo Choi 	{ .name = "max77693-flash", },
4783871c00SChanwoo Choi 	{ .name = "max77693-muic", },
4883871c00SChanwoo Choi 	{ .name = "max77693-haptic", },
4983871c00SChanwoo Choi };
5083871c00SChanwoo Choi 
5183871c00SChanwoo Choi int max77693_read_reg(struct regmap *map, u8 reg, u8 *dest)
5283871c00SChanwoo Choi {
5383871c00SChanwoo Choi 	unsigned int val;
5483871c00SChanwoo Choi 	int ret;
5583871c00SChanwoo Choi 
5683871c00SChanwoo Choi 	ret = regmap_read(map, reg, &val);
5783871c00SChanwoo Choi 	*dest = val;
5883871c00SChanwoo Choi 
5983871c00SChanwoo Choi 	return ret;
6083871c00SChanwoo Choi }
6183871c00SChanwoo Choi EXPORT_SYMBOL_GPL(max77693_read_reg);
6283871c00SChanwoo Choi 
6383871c00SChanwoo Choi int max77693_bulk_read(struct regmap *map, u8 reg, int count, u8 *buf)
6483871c00SChanwoo Choi {
6583871c00SChanwoo Choi 	int ret;
6683871c00SChanwoo Choi 
6783871c00SChanwoo Choi 	ret = regmap_bulk_read(map, reg, buf, count);
6883871c00SChanwoo Choi 
6983871c00SChanwoo Choi 	return ret;
7083871c00SChanwoo Choi }
7183871c00SChanwoo Choi EXPORT_SYMBOL_GPL(max77693_bulk_read);
7283871c00SChanwoo Choi 
7383871c00SChanwoo Choi int max77693_write_reg(struct regmap *map, u8 reg, u8 value)
7483871c00SChanwoo Choi {
7583871c00SChanwoo Choi 	int ret;
7683871c00SChanwoo Choi 
7783871c00SChanwoo Choi 	ret = regmap_write(map, reg, value);
7883871c00SChanwoo Choi 
7983871c00SChanwoo Choi 	return ret;
8083871c00SChanwoo Choi }
8183871c00SChanwoo Choi EXPORT_SYMBOL_GPL(max77693_write_reg);
8283871c00SChanwoo Choi 
8383871c00SChanwoo Choi int max77693_bulk_write(struct regmap *map, u8 reg, int count, u8 *buf)
8483871c00SChanwoo Choi {
8583871c00SChanwoo Choi 	int ret;
8683871c00SChanwoo Choi 
8783871c00SChanwoo Choi 	ret = regmap_bulk_write(map, reg, buf, count);
8883871c00SChanwoo Choi 
8983871c00SChanwoo Choi 	return ret;
9083871c00SChanwoo Choi }
9183871c00SChanwoo Choi EXPORT_SYMBOL_GPL(max77693_bulk_write);
9283871c00SChanwoo Choi 
9383871c00SChanwoo Choi int max77693_update_reg(struct regmap *map, u8 reg, u8 val, u8 mask)
9483871c00SChanwoo Choi {
9583871c00SChanwoo Choi 	int ret;
9683871c00SChanwoo Choi 
9783871c00SChanwoo Choi 	ret = regmap_update_bits(map, reg, mask, val);
9883871c00SChanwoo Choi 
9983871c00SChanwoo Choi 	return ret;
10083871c00SChanwoo Choi }
10183871c00SChanwoo Choi EXPORT_SYMBOL_GPL(max77693_update_reg);
10283871c00SChanwoo Choi 
10383871c00SChanwoo Choi static const struct regmap_config max77693_regmap_config = {
10483871c00SChanwoo Choi 	.reg_bits = 8,
10583871c00SChanwoo Choi 	.val_bits = 8,
10683871c00SChanwoo Choi 	.max_register = MAX77693_PMIC_REG_END,
10783871c00SChanwoo Choi };
10883871c00SChanwoo Choi 
10983871c00SChanwoo Choi static int max77693_i2c_probe(struct i2c_client *i2c,
11083871c00SChanwoo Choi 			      const struct i2c_device_id *id)
11183871c00SChanwoo Choi {
11283871c00SChanwoo Choi 	struct max77693_dev *max77693;
11383871c00SChanwoo Choi 	u8 reg_data;
11483871c00SChanwoo Choi 	int ret = 0;
11583871c00SChanwoo Choi 
11683871c00SChanwoo Choi 	max77693 = devm_kzalloc(&i2c->dev,
11783871c00SChanwoo Choi 			sizeof(struct max77693_dev), GFP_KERNEL);
11883871c00SChanwoo Choi 	if (max77693 == NULL)
11983871c00SChanwoo Choi 		return -ENOMEM;
12083871c00SChanwoo Choi 
12183871c00SChanwoo Choi 	i2c_set_clientdata(i2c, max77693);
12283871c00SChanwoo Choi 	max77693->dev = &i2c->dev;
12383871c00SChanwoo Choi 	max77693->i2c = i2c;
12483871c00SChanwoo Choi 	max77693->irq = i2c->irq;
12583871c00SChanwoo Choi 	max77693->type = id->driver_data;
12683871c00SChanwoo Choi 
1272429d863SAxel Lin 	max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
1282429d863SAxel Lin 	if (IS_ERR(max77693->regmap)) {
1292429d863SAxel Lin 		ret = PTR_ERR(max77693->regmap);
1302429d863SAxel Lin 		dev_err(max77693->dev, "failed to allocate register map: %d\n",
1312429d863SAxel Lin 				ret);
1322429d863SAxel Lin 		return ret;
1332429d863SAxel Lin 	}
13483871c00SChanwoo Choi 
1352429d863SAxel Lin 	ret = max77693_read_reg(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
1362429d863SAxel Lin 				&reg_data);
1372429d863SAxel Lin 	if (ret < 0) {
13883871c00SChanwoo Choi 		dev_err(max77693->dev, "device not found on this channel\n");
1392429d863SAxel Lin 		return ret;
14083871c00SChanwoo Choi 	} else
14183871c00SChanwoo Choi 		dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
14283871c00SChanwoo Choi 
14383871c00SChanwoo Choi 	max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
14483871c00SChanwoo Choi 	i2c_set_clientdata(max77693->muic, max77693);
14583871c00SChanwoo Choi 
14683871c00SChanwoo Choi 	max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
14783871c00SChanwoo Choi 	i2c_set_clientdata(max77693->haptic, max77693);
14883871c00SChanwoo Choi 
149b186b124SChanwoo Choi 	/*
150b186b124SChanwoo Choi 	 * Initialize register map for MUIC device because use regmap-muic
151b186b124SChanwoo Choi 	 * instance of MUIC device when irq of max77693 is initialized
152b186b124SChanwoo Choi 	 * before call max77693-muic probe() function.
153b186b124SChanwoo Choi 	 */
154b186b124SChanwoo Choi 	max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
155b186b124SChanwoo Choi 					 &max77693_regmap_config);
156b186b124SChanwoo Choi 	if (IS_ERR(max77693->regmap_muic)) {
157b186b124SChanwoo Choi 		ret = PTR_ERR(max77693->regmap_muic);
158b186b124SChanwoo Choi 		dev_err(max77693->dev,
159b186b124SChanwoo Choi 			"failed to allocate register map: %d\n", ret);
1602429d863SAxel Lin 		goto err_regmap_muic;
161b186b124SChanwoo Choi 	}
162b186b124SChanwoo Choi 
1636592ebb3SChanwoo Choi 	ret = max77693_irq_init(max77693);
1646592ebb3SChanwoo Choi 	if (ret < 0)
165ff2b7ac6SAxel Lin 		goto err_irq;
1666592ebb3SChanwoo Choi 
16783871c00SChanwoo Choi 	pm_runtime_set_active(max77693->dev);
16883871c00SChanwoo Choi 
16983871c00SChanwoo Choi 	ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
1700848c94fSMark Brown 			      ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
17183871c00SChanwoo Choi 	if (ret < 0)
17283871c00SChanwoo Choi 		goto err_mfd;
17383871c00SChanwoo Choi 
17483871c00SChanwoo Choi 	return ret;
17583871c00SChanwoo Choi 
17683871c00SChanwoo Choi err_mfd:
177ff2b7ac6SAxel Lin 	max77693_irq_exit(max77693);
178ff2b7ac6SAxel Lin err_irq:
1792429d863SAxel Lin err_regmap_muic:
18083871c00SChanwoo Choi 	i2c_unregister_device(max77693->muic);
18183871c00SChanwoo Choi 	i2c_unregister_device(max77693->haptic);
18283871c00SChanwoo Choi 	return ret;
18383871c00SChanwoo Choi }
18483871c00SChanwoo Choi 
18583871c00SChanwoo Choi static int max77693_i2c_remove(struct i2c_client *i2c)
18683871c00SChanwoo Choi {
18783871c00SChanwoo Choi 	struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
18883871c00SChanwoo Choi 
18983871c00SChanwoo Choi 	mfd_remove_devices(max77693->dev);
190ff2b7ac6SAxel Lin 	max77693_irq_exit(max77693);
19183871c00SChanwoo Choi 	i2c_unregister_device(max77693->muic);
19283871c00SChanwoo Choi 	i2c_unregister_device(max77693->haptic);
19383871c00SChanwoo Choi 
19483871c00SChanwoo Choi 	return 0;
19583871c00SChanwoo Choi }
19683871c00SChanwoo Choi 
19783871c00SChanwoo Choi static const struct i2c_device_id max77693_i2c_id[] = {
19883871c00SChanwoo Choi 	{ "max77693", TYPE_MAX77693 },
19983871c00SChanwoo Choi 	{ }
20083871c00SChanwoo Choi };
20183871c00SChanwoo Choi MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
20283871c00SChanwoo Choi 
2036592ebb3SChanwoo Choi static int max77693_suspend(struct device *dev)
2046592ebb3SChanwoo Choi {
2056592ebb3SChanwoo Choi 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
2066592ebb3SChanwoo Choi 	struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
2076592ebb3SChanwoo Choi 
2086592ebb3SChanwoo Choi 	if (device_may_wakeup(dev))
2096592ebb3SChanwoo Choi 		irq_set_irq_wake(max77693->irq, 1);
2106592ebb3SChanwoo Choi 	return 0;
2116592ebb3SChanwoo Choi }
2126592ebb3SChanwoo Choi 
2136592ebb3SChanwoo Choi static int max77693_resume(struct device *dev)
2146592ebb3SChanwoo Choi {
2156592ebb3SChanwoo Choi 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
2166592ebb3SChanwoo Choi 	struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
2176592ebb3SChanwoo Choi 
2186592ebb3SChanwoo Choi 	if (device_may_wakeup(dev))
2196592ebb3SChanwoo Choi 		irq_set_irq_wake(max77693->irq, 0);
2206592ebb3SChanwoo Choi 	return max77693_irq_resume(max77693);
2216592ebb3SChanwoo Choi }
2226592ebb3SChanwoo Choi 
22312477a32SMark Brown static const struct dev_pm_ops max77693_pm = {
2246592ebb3SChanwoo Choi 	.suspend = max77693_suspend,
2256592ebb3SChanwoo Choi 	.resume = max77693_resume,
2266592ebb3SChanwoo Choi };
2276592ebb3SChanwoo Choi 
2284657185cSAndrzej Hajda #ifdef CONFIG_OF
2294657185cSAndrzej Hajda static struct of_device_id max77693_dt_match[] = {
2304657185cSAndrzej Hajda 	{ .compatible = "maxim,max77693" },
2314657185cSAndrzej Hajda 	{},
2324657185cSAndrzej Hajda };
2334657185cSAndrzej Hajda #endif
2344657185cSAndrzej Hajda 
23583871c00SChanwoo Choi static struct i2c_driver max77693_i2c_driver = {
23683871c00SChanwoo Choi 	.driver = {
23783871c00SChanwoo Choi 		   .name = "max77693",
23883871c00SChanwoo Choi 		   .owner = THIS_MODULE,
2396592ebb3SChanwoo Choi 		   .pm = &max77693_pm,
2404657185cSAndrzej Hajda 		   .of_match_table = of_match_ptr(max77693_dt_match),
24183871c00SChanwoo Choi 	},
24283871c00SChanwoo Choi 	.probe = max77693_i2c_probe,
24383871c00SChanwoo Choi 	.remove = max77693_i2c_remove,
24483871c00SChanwoo Choi 	.id_table = max77693_i2c_id,
24583871c00SChanwoo Choi };
24683871c00SChanwoo Choi 
24783871c00SChanwoo Choi static int __init max77693_i2c_init(void)
24883871c00SChanwoo Choi {
24983871c00SChanwoo Choi 	return i2c_add_driver(&max77693_i2c_driver);
25083871c00SChanwoo Choi }
25183871c00SChanwoo Choi /* init early so consumer devices can complete system boot */
25283871c00SChanwoo Choi subsys_initcall(max77693_i2c_init);
25383871c00SChanwoo Choi 
25483871c00SChanwoo Choi static void __exit max77693_i2c_exit(void)
25583871c00SChanwoo Choi {
25683871c00SChanwoo Choi 	i2c_del_driver(&max77693_i2c_driver);
25783871c00SChanwoo Choi }
25883871c00SChanwoo Choi module_exit(max77693_i2c_exit);
25983871c00SChanwoo Choi 
26083871c00SChanwoo Choi MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
26183871c00SChanwoo Choi MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
26283871c00SChanwoo Choi MODULE_LICENSE("GPL");
263