Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mfd: Switch the AB8500 GPADC to IIO

The AB8500 GPADC driver is indeed a "general purpose ADC" driver,
and while the IIO subsystem did not exist when the driver was
first merged, it is never too late to clean things up and move it
to the right place.

Nowadays IIO provides the right abstractions and interfaces to
do generic ADC work in the kernel.

We have to cut a bunch of debugfs luggage to make this transition
swift, but all these files to is read out the raw values of the
ADC and the IIO subsystem already has a standard sysfs ABI for
doing exactly this: no debugfs is needed.

Acked-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Linus Walleij and committed by
Jonathan Cameron
a77fc111 07063bbf

-1873
-7
drivers/mfd/Kconfig
··· 1210 1210 Select this option if you want debug information using the debug 1211 1211 filesystem, debugfs. 1212 1212 1213 - config AB8500_GPADC 1214 - bool "ST-Ericsson AB8500 GPADC driver" 1215 - depends on AB8500_CORE && REGULATOR_AB8500 1216 - default y 1217 - help 1218 - AB8500 GPADC driver used to convert Acc and battery/ac/usb voltage 1219 - 1220 1213 config MFD_DB8500_PRCMU 1221 1214 bool "ST-Ericsson DB8500 Power Reset Control Management Unit" 1222 1215 depends on UX500_SOC_DB8500
-1
drivers/mfd/Makefile
··· 177 177 obj-$(CONFIG_AB3100_CORE) += ab3100-core.o 178 178 obj-$(CONFIG_AB3100_OTP) += ab3100-otp.o 179 179 obj-$(CONFIG_AB8500_DEBUG) += ab8500-debugfs.o 180 - obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o 181 180 obj-$(CONFIG_MFD_DB8500_PRCMU) += db8500-prcmu.o 182 181 # ab8500-core need to come after db8500-prcmu (which provides the channel) 183 182 obj-$(CONFIG_AB8500_CORE) += ab8500-core.o ab8500-sysctrl.o
-715
drivers/mfd/ab8500-debugfs.c
··· 84 84 85 85 #include <linux/mfd/abx500.h> 86 86 #include <linux/mfd/abx500/ab8500.h> 87 - #include <linux/mfd/abx500/ab8500-gpadc.h> 88 87 89 88 #ifdef CONFIG_DEBUG_FS 90 89 #include <linux/string.h> ··· 101 102 102 103 static struct device_attribute **dev_attr; 103 104 static char **event_name; 104 - 105 - static u8 avg_sample = SAMPLE_16; 106 - static u8 trig_edge = RISING_EDGE; 107 - static u8 conv_type = ADC_SW; 108 - static u8 trig_timer; 109 105 110 106 /** 111 107 * struct ab8500_reg_range ··· 146 152 }; 147 153 148 154 #define AB8500_NAME_STRING "ab8500" 149 - #define AB8500_ADC_NAME_STRING "gpadc" 150 155 #define AB8500_NUM_BANKS AB8500_DEBUG_FIELD_LAST 151 156 152 157 #define AB8500_REV_REG 0x80 ··· 1639 1646 1640 1647 DEFINE_SHOW_ATTRIBUTE(ab8500_modem); 1641 1648 1642 - static int ab8500_gpadc_bat_ctrl_show(struct seq_file *s, void *p) 1643 - { 1644 - int bat_ctrl_raw; 1645 - int bat_ctrl_convert; 1646 - struct ab8500_gpadc *gpadc; 1647 - 1648 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1649 - bat_ctrl_raw = ab8500_gpadc_read_raw(gpadc, BAT_CTRL, 1650 - avg_sample, trig_edge, trig_timer, conv_type); 1651 - bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc, 1652 - BAT_CTRL, bat_ctrl_raw); 1653 - 1654 - seq_printf(s, "%d,0x%X\n", bat_ctrl_convert, bat_ctrl_raw); 1655 - 1656 - return 0; 1657 - } 1658 - 1659 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_bat_ctrl); 1660 - 1661 - static int ab8500_gpadc_btemp_ball_show(struct seq_file *s, void *p) 1662 - { 1663 - int btemp_ball_raw; 1664 - int btemp_ball_convert; 1665 - struct ab8500_gpadc *gpadc; 1666 - 1667 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1668 - btemp_ball_raw = ab8500_gpadc_read_raw(gpadc, BTEMP_BALL, 1669 - avg_sample, trig_edge, trig_timer, conv_type); 1670 - btemp_ball_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL, 1671 - btemp_ball_raw); 1672 - 1673 - seq_printf(s, "%d,0x%X\n", btemp_ball_convert, btemp_ball_raw); 1674 - 1675 - return 0; 1676 - } 1677 - 1678 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_btemp_ball); 1679 - 1680 - static int ab8500_gpadc_main_charger_v_show(struct seq_file *s, void *p) 1681 - { 1682 - int main_charger_v_raw; 1683 - int main_charger_v_convert; 1684 - struct ab8500_gpadc *gpadc; 1685 - 1686 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1687 - main_charger_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_V, 1688 - avg_sample, trig_edge, trig_timer, conv_type); 1689 - main_charger_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, 1690 - MAIN_CHARGER_V, main_charger_v_raw); 1691 - 1692 - seq_printf(s, "%d,0x%X\n", main_charger_v_convert, main_charger_v_raw); 1693 - 1694 - return 0; 1695 - } 1696 - 1697 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_main_charger_v); 1698 - 1699 - static int ab8500_gpadc_acc_detect1_show(struct seq_file *s, void *p) 1700 - { 1701 - int acc_detect1_raw; 1702 - int acc_detect1_convert; 1703 - struct ab8500_gpadc *gpadc; 1704 - 1705 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1706 - acc_detect1_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT1, 1707 - avg_sample, trig_edge, trig_timer, conv_type); 1708 - acc_detect1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ACC_DETECT1, 1709 - acc_detect1_raw); 1710 - 1711 - seq_printf(s, "%d,0x%X\n", acc_detect1_convert, acc_detect1_raw); 1712 - 1713 - return 0; 1714 - } 1715 - 1716 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_acc_detect1); 1717 - 1718 - static int ab8500_gpadc_acc_detect2_show(struct seq_file *s, void *p) 1719 - { 1720 - int acc_detect2_raw; 1721 - int acc_detect2_convert; 1722 - struct ab8500_gpadc *gpadc; 1723 - 1724 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1725 - acc_detect2_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT2, 1726 - avg_sample, trig_edge, trig_timer, conv_type); 1727 - acc_detect2_convert = ab8500_gpadc_ad_to_voltage(gpadc, 1728 - ACC_DETECT2, acc_detect2_raw); 1729 - 1730 - seq_printf(s, "%d,0x%X\n", acc_detect2_convert, acc_detect2_raw); 1731 - 1732 - return 0; 1733 - } 1734 - 1735 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_acc_detect2); 1736 - 1737 - static int ab8500_gpadc_aux1_show(struct seq_file *s, void *p) 1738 - { 1739 - int aux1_raw; 1740 - int aux1_convert; 1741 - struct ab8500_gpadc *gpadc; 1742 - 1743 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1744 - aux1_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX1, 1745 - avg_sample, trig_edge, trig_timer, conv_type); 1746 - aux1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX1, 1747 - aux1_raw); 1748 - 1749 - seq_printf(s, "%d,0x%X\n", aux1_convert, aux1_raw); 1750 - 1751 - return 0; 1752 - } 1753 - 1754 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_aux1); 1755 - 1756 - static int ab8500_gpadc_aux2_show(struct seq_file *s, void *p) 1757 - { 1758 - int aux2_raw; 1759 - int aux2_convert; 1760 - struct ab8500_gpadc *gpadc; 1761 - 1762 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1763 - aux2_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX2, 1764 - avg_sample, trig_edge, trig_timer, conv_type); 1765 - aux2_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX2, 1766 - aux2_raw); 1767 - 1768 - seq_printf(s, "%d,0x%X\n", aux2_convert, aux2_raw); 1769 - 1770 - return 0; 1771 - } 1772 - 1773 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_aux2); 1774 - 1775 - static int ab8500_gpadc_main_bat_v_show(struct seq_file *s, void *p) 1776 - { 1777 - int main_bat_v_raw; 1778 - int main_bat_v_convert; 1779 - struct ab8500_gpadc *gpadc; 1780 - 1781 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1782 - main_bat_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_BAT_V, 1783 - avg_sample, trig_edge, trig_timer, conv_type); 1784 - main_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V, 1785 - main_bat_v_raw); 1786 - 1787 - seq_printf(s, "%d,0x%X\n", main_bat_v_convert, main_bat_v_raw); 1788 - 1789 - return 0; 1790 - } 1791 - 1792 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_main_bat_v); 1793 - 1794 - static int ab8500_gpadc_vbus_v_show(struct seq_file *s, void *p) 1795 - { 1796 - int vbus_v_raw; 1797 - int vbus_v_convert; 1798 - struct ab8500_gpadc *gpadc; 1799 - 1800 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1801 - vbus_v_raw = ab8500_gpadc_read_raw(gpadc, VBUS_V, 1802 - avg_sample, trig_edge, trig_timer, conv_type); 1803 - vbus_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, VBUS_V, 1804 - vbus_v_raw); 1805 - 1806 - seq_printf(s, "%d,0x%X\n", vbus_v_convert, vbus_v_raw); 1807 - 1808 - return 0; 1809 - } 1810 - 1811 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_vbus_v); 1812 - 1813 - static int ab8500_gpadc_main_charger_c_show(struct seq_file *s, void *p) 1814 - { 1815 - int main_charger_c_raw; 1816 - int main_charger_c_convert; 1817 - struct ab8500_gpadc *gpadc; 1818 - 1819 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1820 - main_charger_c_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_C, 1821 - avg_sample, trig_edge, trig_timer, conv_type); 1822 - main_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc, 1823 - MAIN_CHARGER_C, main_charger_c_raw); 1824 - 1825 - seq_printf(s, "%d,0x%X\n", main_charger_c_convert, main_charger_c_raw); 1826 - 1827 - return 0; 1828 - } 1829 - 1830 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_main_charger_c); 1831 - 1832 - static int ab8500_gpadc_usb_charger_c_show(struct seq_file *s, void *p) 1833 - { 1834 - int usb_charger_c_raw; 1835 - int usb_charger_c_convert; 1836 - struct ab8500_gpadc *gpadc; 1837 - 1838 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1839 - usb_charger_c_raw = ab8500_gpadc_read_raw(gpadc, USB_CHARGER_C, 1840 - avg_sample, trig_edge, trig_timer, conv_type); 1841 - usb_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc, 1842 - USB_CHARGER_C, usb_charger_c_raw); 1843 - 1844 - seq_printf(s, "%d,0x%X\n", usb_charger_c_convert, usb_charger_c_raw); 1845 - 1846 - return 0; 1847 - } 1848 - 1849 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_usb_charger_c); 1850 - 1851 - static int ab8500_gpadc_bk_bat_v_show(struct seq_file *s, void *p) 1852 - { 1853 - int bk_bat_v_raw; 1854 - int bk_bat_v_convert; 1855 - struct ab8500_gpadc *gpadc; 1856 - 1857 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1858 - bk_bat_v_raw = ab8500_gpadc_read_raw(gpadc, BK_BAT_V, 1859 - avg_sample, trig_edge, trig_timer, conv_type); 1860 - bk_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, 1861 - BK_BAT_V, bk_bat_v_raw); 1862 - 1863 - seq_printf(s, "%d,0x%X\n", bk_bat_v_convert, bk_bat_v_raw); 1864 - 1865 - return 0; 1866 - } 1867 - 1868 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_bk_bat_v); 1869 - 1870 - static int ab8500_gpadc_die_temp_show(struct seq_file *s, void *p) 1871 - { 1872 - int die_temp_raw; 1873 - int die_temp_convert; 1874 - struct ab8500_gpadc *gpadc; 1875 - 1876 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1877 - die_temp_raw = ab8500_gpadc_read_raw(gpadc, DIE_TEMP, 1878 - avg_sample, trig_edge, trig_timer, conv_type); 1879 - die_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, DIE_TEMP, 1880 - die_temp_raw); 1881 - 1882 - seq_printf(s, "%d,0x%X\n", die_temp_convert, die_temp_raw); 1883 - 1884 - return 0; 1885 - } 1886 - 1887 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_die_temp); 1888 - 1889 - static int ab8500_gpadc_usb_id_show(struct seq_file *s, void *p) 1890 - { 1891 - int usb_id_raw; 1892 - int usb_id_convert; 1893 - struct ab8500_gpadc *gpadc; 1894 - 1895 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1896 - usb_id_raw = ab8500_gpadc_read_raw(gpadc, USB_ID, 1897 - avg_sample, trig_edge, trig_timer, conv_type); 1898 - usb_id_convert = ab8500_gpadc_ad_to_voltage(gpadc, USB_ID, 1899 - usb_id_raw); 1900 - 1901 - seq_printf(s, "%d,0x%X\n", usb_id_convert, usb_id_raw); 1902 - 1903 - return 0; 1904 - } 1905 - 1906 - DEFINE_SHOW_ATTRIBUTE(ab8500_gpadc_usb_id); 1907 - 1908 - static int ab8540_gpadc_xtal_temp_show(struct seq_file *s, void *p) 1909 - { 1910 - int xtal_temp_raw; 1911 - int xtal_temp_convert; 1912 - struct ab8500_gpadc *gpadc; 1913 - 1914 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1915 - xtal_temp_raw = ab8500_gpadc_read_raw(gpadc, XTAL_TEMP, 1916 - avg_sample, trig_edge, trig_timer, conv_type); 1917 - xtal_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, XTAL_TEMP, 1918 - xtal_temp_raw); 1919 - 1920 - seq_printf(s, "%d,0x%X\n", xtal_temp_convert, xtal_temp_raw); 1921 - 1922 - return 0; 1923 - } 1924 - 1925 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_xtal_temp); 1926 - 1927 - static int ab8540_gpadc_vbat_true_meas_show(struct seq_file *s, void *p) 1928 - { 1929 - int vbat_true_meas_raw; 1930 - int vbat_true_meas_convert; 1931 - struct ab8500_gpadc *gpadc; 1932 - 1933 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1934 - vbat_true_meas_raw = ab8500_gpadc_read_raw(gpadc, VBAT_TRUE_MEAS, 1935 - avg_sample, trig_edge, trig_timer, conv_type); 1936 - vbat_true_meas_convert = 1937 - ab8500_gpadc_ad_to_voltage(gpadc, VBAT_TRUE_MEAS, 1938 - vbat_true_meas_raw); 1939 - 1940 - seq_printf(s, "%d,0x%X\n", vbat_true_meas_convert, vbat_true_meas_raw); 1941 - 1942 - return 0; 1943 - } 1944 - 1945 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_vbat_true_meas); 1946 - 1947 - static int ab8540_gpadc_bat_ctrl_and_ibat_show(struct seq_file *s, void *p) 1948 - { 1949 - int bat_ctrl_raw; 1950 - int bat_ctrl_convert; 1951 - int ibat_raw; 1952 - int ibat_convert; 1953 - struct ab8500_gpadc *gpadc; 1954 - 1955 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1956 - bat_ctrl_raw = ab8500_gpadc_double_read_raw(gpadc, BAT_CTRL_AND_IBAT, 1957 - avg_sample, trig_edge, trig_timer, conv_type, &ibat_raw); 1958 - 1959 - bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc, BAT_CTRL, 1960 - bat_ctrl_raw); 1961 - ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL, 1962 - ibat_raw); 1963 - 1964 - seq_printf(s, 1965 - "%d,0x%X\n" 1966 - "%d,0x%X\n", 1967 - bat_ctrl_convert, bat_ctrl_raw, 1968 - ibat_convert, ibat_raw); 1969 - 1970 - return 0; 1971 - } 1972 - 1973 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_bat_ctrl_and_ibat); 1974 - 1975 - static int ab8540_gpadc_vbat_meas_and_ibat_show(struct seq_file *s, void *p) 1976 - { 1977 - int vbat_meas_raw; 1978 - int vbat_meas_convert; 1979 - int ibat_raw; 1980 - int ibat_convert; 1981 - struct ab8500_gpadc *gpadc; 1982 - 1983 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 1984 - vbat_meas_raw = ab8500_gpadc_double_read_raw(gpadc, VBAT_MEAS_AND_IBAT, 1985 - avg_sample, trig_edge, trig_timer, conv_type, &ibat_raw); 1986 - vbat_meas_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V, 1987 - vbat_meas_raw); 1988 - ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL, 1989 - ibat_raw); 1990 - 1991 - seq_printf(s, 1992 - "%d,0x%X\n" 1993 - "%d,0x%X\n", 1994 - vbat_meas_convert, vbat_meas_raw, 1995 - ibat_convert, ibat_raw); 1996 - 1997 - return 0; 1998 - } 1999 - 2000 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_vbat_meas_and_ibat); 2001 - 2002 - static int ab8540_gpadc_vbat_true_meas_and_ibat_show(struct seq_file *s, void *p) 2003 - { 2004 - int vbat_true_meas_raw; 2005 - int vbat_true_meas_convert; 2006 - int ibat_raw; 2007 - int ibat_convert; 2008 - struct ab8500_gpadc *gpadc; 2009 - 2010 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2011 - vbat_true_meas_raw = ab8500_gpadc_double_read_raw(gpadc, 2012 - VBAT_TRUE_MEAS_AND_IBAT, avg_sample, trig_edge, 2013 - trig_timer, conv_type, &ibat_raw); 2014 - vbat_true_meas_convert = ab8500_gpadc_ad_to_voltage(gpadc, 2015 - VBAT_TRUE_MEAS, vbat_true_meas_raw); 2016 - ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL, 2017 - ibat_raw); 2018 - 2019 - seq_printf(s, 2020 - "%d,0x%X\n" 2021 - "%d,0x%X\n", 2022 - vbat_true_meas_convert, vbat_true_meas_raw, 2023 - ibat_convert, ibat_raw); 2024 - 2025 - return 0; 2026 - } 2027 - 2028 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_vbat_true_meas_and_ibat); 2029 - 2030 - static int ab8540_gpadc_bat_temp_and_ibat_show(struct seq_file *s, void *p) 2031 - { 2032 - int bat_temp_raw; 2033 - int bat_temp_convert; 2034 - int ibat_raw; 2035 - int ibat_convert; 2036 - struct ab8500_gpadc *gpadc; 2037 - 2038 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2039 - bat_temp_raw = ab8500_gpadc_double_read_raw(gpadc, BAT_TEMP_AND_IBAT, 2040 - avg_sample, trig_edge, trig_timer, conv_type, &ibat_raw); 2041 - bat_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL, 2042 - bat_temp_raw); 2043 - ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL, 2044 - ibat_raw); 2045 - 2046 - seq_printf(s, 2047 - "%d,0x%X\n" 2048 - "%d,0x%X\n", 2049 - bat_temp_convert, bat_temp_raw, 2050 - ibat_convert, ibat_raw); 2051 - 2052 - return 0; 2053 - } 2054 - 2055 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_bat_temp_and_ibat); 2056 - 2057 - static int ab8540_gpadc_otp_calib_show(struct seq_file *s, void *p) 2058 - { 2059 - struct ab8500_gpadc *gpadc; 2060 - u16 vmain_l, vmain_h, btemp_l, btemp_h; 2061 - u16 vbat_l, vbat_h, ibat_l, ibat_h; 2062 - 2063 - gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2064 - ab8540_gpadc_get_otp(gpadc, &vmain_l, &vmain_h, &btemp_l, &btemp_h, 2065 - &vbat_l, &vbat_h, &ibat_l, &ibat_h); 2066 - seq_printf(s, 2067 - "VMAIN_L:0x%X\n" 2068 - "VMAIN_H:0x%X\n" 2069 - "BTEMP_L:0x%X\n" 2070 - "BTEMP_H:0x%X\n" 2071 - "VBAT_L:0x%X\n" 2072 - "VBAT_H:0x%X\n" 2073 - "IBAT_L:0x%X\n" 2074 - "IBAT_H:0x%X\n", 2075 - vmain_l, vmain_h, btemp_l, btemp_h, 2076 - vbat_l, vbat_h, ibat_l, ibat_h); 2077 - 2078 - return 0; 2079 - } 2080 - 2081 - DEFINE_SHOW_ATTRIBUTE(ab8540_gpadc_otp_calib); 2082 - 2083 - static int ab8500_gpadc_avg_sample_print(struct seq_file *s, void *p) 2084 - { 2085 - seq_printf(s, "%d\n", avg_sample); 2086 - 2087 - return 0; 2088 - } 2089 - 2090 - static int ab8500_gpadc_avg_sample_open(struct inode *inode, struct file *file) 2091 - { 2092 - return single_open(file, ab8500_gpadc_avg_sample_print, 2093 - inode->i_private); 2094 - } 2095 - 2096 - static ssize_t ab8500_gpadc_avg_sample_write(struct file *file, 2097 - const char __user *user_buf, 2098 - size_t count, loff_t *ppos) 2099 - { 2100 - struct device *dev = ((struct seq_file *)(file->private_data))->private; 2101 - unsigned long user_avg_sample; 2102 - int err; 2103 - 2104 - err = kstrtoul_from_user(user_buf, count, 0, &user_avg_sample); 2105 - if (err) 2106 - return err; 2107 - 2108 - if ((user_avg_sample == SAMPLE_1) || (user_avg_sample == SAMPLE_4) 2109 - || (user_avg_sample == SAMPLE_8) 2110 - || (user_avg_sample == SAMPLE_16)) { 2111 - avg_sample = (u8) user_avg_sample; 2112 - } else { 2113 - dev_err(dev, 2114 - "debugfs err input: should be egal to 1, 4, 8 or 16\n"); 2115 - return -EINVAL; 2116 - } 2117 - 2118 - return count; 2119 - } 2120 - 2121 - static const struct file_operations ab8500_gpadc_avg_sample_fops = { 2122 - .open = ab8500_gpadc_avg_sample_open, 2123 - .read = seq_read, 2124 - .write = ab8500_gpadc_avg_sample_write, 2125 - .llseek = seq_lseek, 2126 - .release = single_release, 2127 - .owner = THIS_MODULE, 2128 - }; 2129 - 2130 - static int ab8500_gpadc_trig_edge_print(struct seq_file *s, void *p) 2131 - { 2132 - seq_printf(s, "%d\n", trig_edge); 2133 - 2134 - return 0; 2135 - } 2136 - 2137 - static int ab8500_gpadc_trig_edge_open(struct inode *inode, struct file *file) 2138 - { 2139 - return single_open(file, ab8500_gpadc_trig_edge_print, 2140 - inode->i_private); 2141 - } 2142 - 2143 - static ssize_t ab8500_gpadc_trig_edge_write(struct file *file, 2144 - const char __user *user_buf, 2145 - size_t count, loff_t *ppos) 2146 - { 2147 - struct device *dev = ((struct seq_file *)(file->private_data))->private; 2148 - unsigned long user_trig_edge; 2149 - int err; 2150 - 2151 - err = kstrtoul_from_user(user_buf, count, 0, &user_trig_edge); 2152 - if (err) 2153 - return err; 2154 - 2155 - if ((user_trig_edge == RISING_EDGE) 2156 - || (user_trig_edge == FALLING_EDGE)) { 2157 - trig_edge = (u8) user_trig_edge; 2158 - } else { 2159 - dev_err(dev, "Wrong input:\n" 2160 - "Enter 0. Rising edge\n" 2161 - "Enter 1. Falling edge\n"); 2162 - return -EINVAL; 2163 - } 2164 - 2165 - return count; 2166 - } 2167 - 2168 - static const struct file_operations ab8500_gpadc_trig_edge_fops = { 2169 - .open = ab8500_gpadc_trig_edge_open, 2170 - .read = seq_read, 2171 - .write = ab8500_gpadc_trig_edge_write, 2172 - .llseek = seq_lseek, 2173 - .release = single_release, 2174 - .owner = THIS_MODULE, 2175 - }; 2176 - 2177 - static int ab8500_gpadc_trig_timer_print(struct seq_file *s, void *p) 2178 - { 2179 - seq_printf(s, "%d\n", trig_timer); 2180 - 2181 - return 0; 2182 - } 2183 - 2184 - static int ab8500_gpadc_trig_timer_open(struct inode *inode, struct file *file) 2185 - { 2186 - return single_open(file, ab8500_gpadc_trig_timer_print, 2187 - inode->i_private); 2188 - } 2189 - 2190 - static ssize_t ab8500_gpadc_trig_timer_write(struct file *file, 2191 - const char __user *user_buf, 2192 - size_t count, loff_t *ppos) 2193 - { 2194 - struct device *dev = ((struct seq_file *)(file->private_data))->private; 2195 - unsigned long user_trig_timer; 2196 - int err; 2197 - 2198 - err = kstrtoul_from_user(user_buf, count, 0, &user_trig_timer); 2199 - if (err) 2200 - return err; 2201 - 2202 - if (user_trig_timer & ~0xFF) { 2203 - dev_err(dev, 2204 - "debugfs error input: should be between 0 to 255\n"); 2205 - return -EINVAL; 2206 - } 2207 - 2208 - trig_timer = (u8) user_trig_timer; 2209 - 2210 - return count; 2211 - } 2212 - 2213 - static const struct file_operations ab8500_gpadc_trig_timer_fops = { 2214 - .open = ab8500_gpadc_trig_timer_open, 2215 - .read = seq_read, 2216 - .write = ab8500_gpadc_trig_timer_write, 2217 - .llseek = seq_lseek, 2218 - .release = single_release, 2219 - .owner = THIS_MODULE, 2220 - }; 2221 - 2222 - static int ab8500_gpadc_conv_type_print(struct seq_file *s, void *p) 2223 - { 2224 - seq_printf(s, "%d\n", conv_type); 2225 - 2226 - return 0; 2227 - } 2228 - 2229 - static int ab8500_gpadc_conv_type_open(struct inode *inode, struct file *file) 2230 - { 2231 - return single_open(file, ab8500_gpadc_conv_type_print, 2232 - inode->i_private); 2233 - } 2234 - 2235 - static ssize_t ab8500_gpadc_conv_type_write(struct file *file, 2236 - const char __user *user_buf, 2237 - size_t count, loff_t *ppos) 2238 - { 2239 - struct device *dev = ((struct seq_file *)(file->private_data))->private; 2240 - unsigned long user_conv_type; 2241 - int err; 2242 - 2243 - err = kstrtoul_from_user(user_buf, count, 0, &user_conv_type); 2244 - if (err) 2245 - return err; 2246 - 2247 - if ((user_conv_type == ADC_SW) 2248 - || (user_conv_type == ADC_HW)) { 2249 - conv_type = (u8) user_conv_type; 2250 - } else { 2251 - dev_err(dev, "Wrong input:\n" 2252 - "Enter 0. ADC SW conversion\n" 2253 - "Enter 1. ADC HW conversion\n"); 2254 - return -EINVAL; 2255 - } 2256 - 2257 - return count; 2258 - } 2259 - 2260 - static const struct file_operations ab8500_gpadc_conv_type_fops = { 2261 - .open = ab8500_gpadc_conv_type_open, 2262 - .read = seq_read, 2263 - .write = ab8500_gpadc_conv_type_write, 2264 - .llseek = seq_lseek, 2265 - .release = single_release, 2266 - .owner = THIS_MODULE, 2267 - }; 2268 - 2269 1649 /* 2270 1650 * return length of an ASCII numerical value, 0 is string is not a 2271 1651 * numerical value. ··· 2013 2647 static int ab8500_debug_probe(struct platform_device *plf) 2014 2648 { 2015 2649 struct dentry *ab8500_dir; 2016 - struct dentry *ab8500_gpadc_dir; 2017 2650 struct ab8500 *ab8500; 2018 2651 struct resource *res; 2019 2652 ··· 2054 2689 2055 2690 ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL); 2056 2691 2057 - ab8500_gpadc_dir = debugfs_create_dir(AB8500_ADC_NAME_STRING, 2058 - ab8500_dir); 2059 - 2060 2692 debugfs_create_file("all-bank-registers", S_IRUGO, ab8500_dir, 2061 2693 &plf->dev, &ab8500_bank_registers_fops); 2062 2694 debugfs_create_file("all-banks", S_IRUGO, ab8500_dir, ··· 2089 2727 &plf->dev, &ab8500_hwreg_fops); 2090 2728 debugfs_create_file("all-modem-registers", (S_IRUGO | S_IWUSR | S_IWGRP), 2091 2729 ab8500_dir, &plf->dev, &ab8500_modem_fops); 2092 - debugfs_create_file("bat_ctrl", (S_IRUGO | S_IWUSR | S_IWGRP), 2093 - ab8500_gpadc_dir, &plf->dev, 2094 - &ab8500_gpadc_bat_ctrl_fops); 2095 - debugfs_create_file("btemp_ball", (S_IRUGO | S_IWUSR | S_IWGRP), 2096 - ab8500_gpadc_dir, &plf->dev, 2097 - &ab8500_gpadc_btemp_ball_fops); 2098 - debugfs_create_file("main_charger_v", (S_IRUGO | S_IWUSR | S_IWGRP), 2099 - ab8500_gpadc_dir, &plf->dev, 2100 - &ab8500_gpadc_main_charger_v_fops); 2101 - debugfs_create_file("acc_detect1", (S_IRUGO | S_IWUSR | S_IWGRP), 2102 - ab8500_gpadc_dir, &plf->dev, 2103 - &ab8500_gpadc_acc_detect1_fops); 2104 - debugfs_create_file("acc_detect2", (S_IRUGO | S_IWUSR | S_IWGRP), 2105 - ab8500_gpadc_dir, &plf->dev, 2106 - &ab8500_gpadc_acc_detect2_fops); 2107 - debugfs_create_file("adc_aux1", (S_IRUGO | S_IWUSR | S_IWGRP), 2108 - ab8500_gpadc_dir, &plf->dev, 2109 - &ab8500_gpadc_aux1_fops); 2110 - debugfs_create_file("adc_aux2", (S_IRUGO | S_IWUSR | S_IWGRP), 2111 - ab8500_gpadc_dir, &plf->dev, 2112 - &ab8500_gpadc_aux2_fops); 2113 - debugfs_create_file("main_bat_v", (S_IRUGO | S_IWUSR | S_IWGRP), 2114 - ab8500_gpadc_dir, &plf->dev, 2115 - &ab8500_gpadc_main_bat_v_fops); 2116 - debugfs_create_file("vbus_v", (S_IRUGO | S_IWUSR | S_IWGRP), 2117 - ab8500_gpadc_dir, &plf->dev, 2118 - &ab8500_gpadc_vbus_v_fops); 2119 - debugfs_create_file("main_charger_c", (S_IRUGO | S_IWUSR | S_IWGRP), 2120 - ab8500_gpadc_dir, &plf->dev, 2121 - &ab8500_gpadc_main_charger_c_fops); 2122 - debugfs_create_file("usb_charger_c", (S_IRUGO | S_IWUSR | S_IWGRP), 2123 - ab8500_gpadc_dir, &plf->dev, 2124 - &ab8500_gpadc_usb_charger_c_fops); 2125 - debugfs_create_file("bk_bat_v", (S_IRUGO | S_IWUSR | S_IWGRP), 2126 - ab8500_gpadc_dir, &plf->dev, 2127 - &ab8500_gpadc_bk_bat_v_fops); 2128 - debugfs_create_file("die_temp", (S_IRUGO | S_IWUSR | S_IWGRP), 2129 - ab8500_gpadc_dir, &plf->dev, 2130 - &ab8500_gpadc_die_temp_fops); 2131 - debugfs_create_file("usb_id", (S_IRUGO | S_IWUSR | S_IWGRP), 2132 - ab8500_gpadc_dir, &plf->dev, 2133 - &ab8500_gpadc_usb_id_fops); 2134 - if (is_ab8540(ab8500)) { 2135 - debugfs_create_file("xtal_temp", (S_IRUGO | S_IWUSR | S_IWGRP), 2136 - ab8500_gpadc_dir, &plf->dev, 2137 - &ab8540_gpadc_xtal_temp_fops); 2138 - debugfs_create_file("vbattruemeas", (S_IRUGO | S_IWUSR | S_IWGRP), 2139 - ab8500_gpadc_dir, &plf->dev, 2140 - &ab8540_gpadc_vbat_true_meas_fops); 2141 - debugfs_create_file("batctrl_and_ibat", (S_IRUGO | S_IWUGO), 2142 - ab8500_gpadc_dir, &plf->dev, 2143 - &ab8540_gpadc_bat_ctrl_and_ibat_fops); 2144 - debugfs_create_file("vbatmeas_and_ibat", (S_IRUGO | S_IWUGO), 2145 - ab8500_gpadc_dir, &plf->dev, 2146 - &ab8540_gpadc_vbat_meas_and_ibat_fops); 2147 - debugfs_create_file("vbattruemeas_and_ibat", (S_IRUGO | S_IWUGO), 2148 - ab8500_gpadc_dir, &plf->dev, 2149 - &ab8540_gpadc_vbat_true_meas_and_ibat_fops); 2150 - debugfs_create_file("battemp_and_ibat", (S_IRUGO | S_IWUGO), 2151 - ab8500_gpadc_dir, &plf->dev, 2152 - &ab8540_gpadc_bat_temp_and_ibat_fops); 2153 - debugfs_create_file("otp_calib", (S_IRUGO | S_IWUSR | S_IWGRP), 2154 - ab8500_gpadc_dir, &plf->dev, 2155 - &ab8540_gpadc_otp_calib_fops); 2156 - } 2157 - debugfs_create_file("avg_sample", (S_IRUGO | S_IWUSR | S_IWGRP), 2158 - ab8500_gpadc_dir, &plf->dev, 2159 - &ab8500_gpadc_avg_sample_fops); 2160 - debugfs_create_file("trig_edge", (S_IRUGO | S_IWUSR | S_IWGRP), 2161 - ab8500_gpadc_dir, &plf->dev, 2162 - &ab8500_gpadc_trig_edge_fops); 2163 - debugfs_create_file("trig_timer", (S_IRUGO | S_IWUSR | S_IWGRP), 2164 - ab8500_gpadc_dir, &plf->dev, 2165 - &ab8500_gpadc_trig_timer_fops); 2166 - debugfs_create_file("conv_type", (S_IRUGO | S_IWUSR | S_IWGRP), 2167 - ab8500_gpadc_dir, &plf->dev, 2168 - &ab8500_gpadc_conv_type_fops); 2169 2730 2170 2731 return 0; 2171 2732 }
-1075
drivers/mfd/ab8500-gpadc.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * Copyright (C) ST-Ericsson SA 2010 4 - * 5 - * Author: Arun R Murthy <arun.murthy@stericsson.com> 6 - * Author: Daniel Willerud <daniel.willerud@stericsson.com> 7 - * Author: Johan Palsson <johan.palsson@stericsson.com> 8 - * Author: M'boumba Cedric Madianga 9 - */ 10 - #include <linux/init.h> 11 - #include <linux/device.h> 12 - #include <linux/interrupt.h> 13 - #include <linux/spinlock.h> 14 - #include <linux/delay.h> 15 - #include <linux/pm_runtime.h> 16 - #include <linux/platform_device.h> 17 - #include <linux/completion.h> 18 - #include <linux/regulator/consumer.h> 19 - #include <linux/err.h> 20 - #include <linux/slab.h> 21 - #include <linux/list.h> 22 - #include <linux/mfd/abx500.h> 23 - #include <linux/mfd/abx500/ab8500.h> 24 - #include <linux/mfd/abx500/ab8500-gpadc.h> 25 - 26 - /* 27 - * GPADC register offsets 28 - * Bank : 0x0A 29 - */ 30 - #define AB8500_GPADC_CTRL1_REG 0x00 31 - #define AB8500_GPADC_CTRL2_REG 0x01 32 - #define AB8500_GPADC_CTRL3_REG 0x02 33 - #define AB8500_GPADC_AUTO_TIMER_REG 0x03 34 - #define AB8500_GPADC_STAT_REG 0x04 35 - #define AB8500_GPADC_MANDATAL_REG 0x05 36 - #define AB8500_GPADC_MANDATAH_REG 0x06 37 - #define AB8500_GPADC_AUTODATAL_REG 0x07 38 - #define AB8500_GPADC_AUTODATAH_REG 0x08 39 - #define AB8500_GPADC_MUX_CTRL_REG 0x09 40 - #define AB8540_GPADC_MANDATA2L_REG 0x09 41 - #define AB8540_GPADC_MANDATA2H_REG 0x0A 42 - #define AB8540_GPADC_APEAAX_REG 0x10 43 - #define AB8540_GPADC_APEAAT_REG 0x11 44 - #define AB8540_GPADC_APEAAM_REG 0x12 45 - #define AB8540_GPADC_APEAAH_REG 0x13 46 - #define AB8540_GPADC_APEAAL_REG 0x14 47 - 48 - /* 49 - * OTP register offsets 50 - * Bank : 0x15 51 - */ 52 - #define AB8500_GPADC_CAL_1 0x0F 53 - #define AB8500_GPADC_CAL_2 0x10 54 - #define AB8500_GPADC_CAL_3 0x11 55 - #define AB8500_GPADC_CAL_4 0x12 56 - #define AB8500_GPADC_CAL_5 0x13 57 - #define AB8500_GPADC_CAL_6 0x14 58 - #define AB8500_GPADC_CAL_7 0x15 59 - /* New calibration for 8540 */ 60 - #define AB8540_GPADC_OTP4_REG_7 0x38 61 - #define AB8540_GPADC_OTP4_REG_6 0x39 62 - #define AB8540_GPADC_OTP4_REG_5 0x3A 63 - 64 - /* gpadc constants */ 65 - #define EN_VINTCORE12 0x04 66 - #define EN_VTVOUT 0x02 67 - #define EN_GPADC 0x01 68 - #define DIS_GPADC 0x00 69 - #define AVG_1 0x00 70 - #define AVG_4 0x20 71 - #define AVG_8 0x40 72 - #define AVG_16 0x60 73 - #define ADC_SW_CONV 0x04 74 - #define EN_ICHAR 0x80 75 - #define BTEMP_PULL_UP 0x08 76 - #define EN_BUF 0x40 77 - #define DIS_ZERO 0x00 78 - #define GPADC_BUSY 0x01 79 - #define EN_FALLING 0x10 80 - #define EN_TRIG_EDGE 0x02 81 - #define EN_VBIAS_XTAL_TEMP 0x02 82 - 83 - /* GPADC constants from AB8500 spec, UM0836 */ 84 - #define ADC_RESOLUTION 1024 85 - #define ADC_CH_BTEMP_MIN 0 86 - #define ADC_CH_BTEMP_MAX 1350 87 - #define ADC_CH_DIETEMP_MIN 0 88 - #define ADC_CH_DIETEMP_MAX 1350 89 - #define ADC_CH_CHG_V_MIN 0 90 - #define ADC_CH_CHG_V_MAX 20030 91 - #define ADC_CH_ACCDET2_MIN 0 92 - #define ADC_CH_ACCDET2_MAX 2500 93 - #define ADC_CH_VBAT_MIN 2300 94 - #define ADC_CH_VBAT_MAX 4800 95 - #define ADC_CH_CHG_I_MIN 0 96 - #define ADC_CH_CHG_I_MAX 1500 97 - #define ADC_CH_BKBAT_MIN 0 98 - #define ADC_CH_BKBAT_MAX 3200 99 - 100 - /* GPADC constants from AB8540 spec */ 101 - #define ADC_CH_IBAT_MIN (-6000) /* mA range measured by ADC for ibat */ 102 - #define ADC_CH_IBAT_MAX 6000 103 - #define ADC_CH_IBAT_MIN_V (-60) /* mV range measured by ADC for ibat */ 104 - #define ADC_CH_IBAT_MAX_V 60 105 - #define IBAT_VDROP_L (-56) /* mV */ 106 - #define IBAT_VDROP_H 56 107 - 108 - /* This is used to not lose precision when dividing to get gain and offset */ 109 - #define CALIB_SCALE 1000 110 - /* 111 - * Number of bits shift used to not lose precision 112 - * when dividing to get ibat gain. 113 - */ 114 - #define CALIB_SHIFT_IBAT 20 115 - 116 - /* Time in ms before disabling regulator */ 117 - #define GPADC_AUDOSUSPEND_DELAY 1 118 - 119 - #define CONVERSION_TIME 500 /* ms */ 120 - 121 - enum cal_channels { 122 - ADC_INPUT_VMAIN = 0, 123 - ADC_INPUT_BTEMP, 124 - ADC_INPUT_VBAT, 125 - ADC_INPUT_IBAT, 126 - NBR_CAL_INPUTS, 127 - }; 128 - 129 - /** 130 - * struct adc_cal_data - Table for storing gain and offset for the calibrated 131 - * ADC channels 132 - * @gain: Gain of the ADC channel 133 - * @offset: Offset of the ADC channel 134 - */ 135 - struct adc_cal_data { 136 - s64 gain; 137 - s64 offset; 138 - u16 otp_calib_hi; 139 - u16 otp_calib_lo; 140 - }; 141 - 142 - /** 143 - * struct ab8500_gpadc - AB8500 GPADC device information 144 - * @dev: pointer to the struct device 145 - * @node: a list of AB8500 GPADCs, hence prepared for 146 - reentrance 147 - * @parent: pointer to the struct ab8500 148 - * @ab8500_gpadc_complete: pointer to the struct completion, to indicate 149 - * the completion of gpadc conversion 150 - * @ab8500_gpadc_lock: structure of type mutex 151 - * @regu: pointer to the struct regulator 152 - * @irq_sw: interrupt number that is used by gpadc for Sw 153 - * conversion 154 - * @irq_hw: interrupt number that is used by gpadc for Hw 155 - * conversion 156 - * @cal_data array of ADC calibration data structs 157 - */ 158 - struct ab8500_gpadc { 159 - struct device *dev; 160 - struct list_head node; 161 - struct ab8500 *parent; 162 - struct completion ab8500_gpadc_complete; 163 - struct mutex ab8500_gpadc_lock; 164 - struct regulator *regu; 165 - int irq_sw; 166 - int irq_hw; 167 - struct adc_cal_data cal_data[NBR_CAL_INPUTS]; 168 - }; 169 - 170 - static LIST_HEAD(ab8500_gpadc_list); 171 - 172 - /** 173 - * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC 174 - * (i.e. the first GPADC in the instance list) 175 - */ 176 - struct ab8500_gpadc *ab8500_gpadc_get(char *name) 177 - { 178 - struct ab8500_gpadc *gpadc; 179 - 180 - list_for_each_entry(gpadc, &ab8500_gpadc_list, node) { 181 - if (!strcmp(name, dev_name(gpadc->dev))) 182 - return gpadc; 183 - } 184 - 185 - return ERR_PTR(-ENOENT); 186 - } 187 - EXPORT_SYMBOL(ab8500_gpadc_get); 188 - 189 - /** 190 - * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage 191 - */ 192 - int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel, 193 - int ad_value) 194 - { 195 - int res; 196 - 197 - switch (channel) { 198 - case MAIN_CHARGER_V: 199 - /* For some reason we don't have calibrated data */ 200 - if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) { 201 - res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX - 202 - ADC_CH_CHG_V_MIN) * ad_value / 203 - ADC_RESOLUTION; 204 - break; 205 - } 206 - /* Here we can use the calibrated data */ 207 - res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain + 208 - gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE; 209 - break; 210 - 211 - case XTAL_TEMP: 212 - case BAT_CTRL: 213 - case BTEMP_BALL: 214 - case ACC_DETECT1: 215 - case ADC_AUX1: 216 - case ADC_AUX2: 217 - /* For some reason we don't have calibrated data */ 218 - if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) { 219 - res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX - 220 - ADC_CH_BTEMP_MIN) * ad_value / 221 - ADC_RESOLUTION; 222 - break; 223 - } 224 - /* Here we can use the calibrated data */ 225 - res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain + 226 - gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE; 227 - break; 228 - 229 - case MAIN_BAT_V: 230 - case VBAT_TRUE_MEAS: 231 - /* For some reason we don't have calibrated data */ 232 - if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) { 233 - res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX - 234 - ADC_CH_VBAT_MIN) * ad_value / 235 - ADC_RESOLUTION; 236 - break; 237 - } 238 - /* Here we can use the calibrated data */ 239 - res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain + 240 - gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE; 241 - break; 242 - 243 - case DIE_TEMP: 244 - res = ADC_CH_DIETEMP_MIN + 245 - (ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value / 246 - ADC_RESOLUTION; 247 - break; 248 - 249 - case ACC_DETECT2: 250 - res = ADC_CH_ACCDET2_MIN + 251 - (ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value / 252 - ADC_RESOLUTION; 253 - break; 254 - 255 - case VBUS_V: 256 - res = ADC_CH_CHG_V_MIN + 257 - (ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value / 258 - ADC_RESOLUTION; 259 - break; 260 - 261 - case MAIN_CHARGER_C: 262 - case USB_CHARGER_C: 263 - res = ADC_CH_CHG_I_MIN + 264 - (ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value / 265 - ADC_RESOLUTION; 266 - break; 267 - 268 - case BK_BAT_V: 269 - res = ADC_CH_BKBAT_MIN + 270 - (ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value / 271 - ADC_RESOLUTION; 272 - break; 273 - 274 - case IBAT_VIRTUAL_CHANNEL: 275 - /* For some reason we don't have calibrated data */ 276 - if (!gpadc->cal_data[ADC_INPUT_IBAT].gain) { 277 - res = ADC_CH_IBAT_MIN + (ADC_CH_IBAT_MAX - 278 - ADC_CH_IBAT_MIN) * ad_value / 279 - ADC_RESOLUTION; 280 - break; 281 - } 282 - /* Here we can use the calibrated data */ 283 - res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_IBAT].gain + 284 - gpadc->cal_data[ADC_INPUT_IBAT].offset) 285 - >> CALIB_SHIFT_IBAT; 286 - break; 287 - 288 - default: 289 - dev_err(gpadc->dev, 290 - "unknown channel, not possible to convert\n"); 291 - res = -EINVAL; 292 - break; 293 - 294 - } 295 - return res; 296 - } 297 - EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage); 298 - 299 - /** 300 - * ab8500_gpadc_sw_hw_convert() - gpadc conversion 301 - * @channel: analog channel to be converted to digital data 302 - * @avg_sample: number of ADC sample to average 303 - * @trig_egde: selected ADC trig edge 304 - * @trig_timer: selected ADC trigger delay timer 305 - * @conv_type: selected conversion type (HW or SW conversion) 306 - * 307 - * This function converts the selected analog i/p to digital 308 - * data. 309 - */ 310 - int ab8500_gpadc_sw_hw_convert(struct ab8500_gpadc *gpadc, u8 channel, 311 - u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type) 312 - { 313 - int ad_value; 314 - int voltage; 315 - 316 - ad_value = ab8500_gpadc_read_raw(gpadc, channel, avg_sample, 317 - trig_edge, trig_timer, conv_type); 318 - 319 - /* On failure retry a second time */ 320 - if (ad_value < 0) 321 - ad_value = ab8500_gpadc_read_raw(gpadc, channel, avg_sample, 322 - trig_edge, trig_timer, conv_type); 323 - if (ad_value < 0) { 324 - dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n", 325 - channel); 326 - return ad_value; 327 - } 328 - 329 - voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value); 330 - if (voltage < 0) 331 - dev_err(gpadc->dev, 332 - "GPADC to voltage conversion failed ch: %d AD: 0x%x\n", 333 - channel, ad_value); 334 - 335 - return voltage; 336 - } 337 - EXPORT_SYMBOL(ab8500_gpadc_sw_hw_convert); 338 - 339 - /** 340 - * ab8500_gpadc_read_raw() - gpadc read 341 - * @channel: analog channel to be read 342 - * @avg_sample: number of ADC sample to average 343 - * @trig_edge: selected trig edge 344 - * @trig_timer: selected ADC trigger delay timer 345 - * @conv_type: selected conversion type (HW or SW conversion) 346 - * 347 - * This function obtains the raw ADC value for an hardware conversion, 348 - * this then needs to be converted by calling ab8500_gpadc_ad_to_voltage() 349 - */ 350 - int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel, 351 - u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type) 352 - { 353 - return ab8500_gpadc_double_read_raw(gpadc, channel, avg_sample, 354 - trig_edge, trig_timer, conv_type, 355 - NULL); 356 - } 357 - 358 - int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, 359 - u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type, 360 - int *ibat) 361 - { 362 - int ret; 363 - int looplimit = 0; 364 - unsigned long completion_timeout; 365 - u8 val, low_data, high_data, low_data2, high_data2; 366 - u8 val_reg1 = 0; 367 - unsigned int delay_min = 0; 368 - unsigned int delay_max = 0; 369 - u8 data_low_addr, data_high_addr; 370 - 371 - if (!gpadc) 372 - return -ENODEV; 373 - 374 - /* check if convertion is supported */ 375 - if ((gpadc->irq_sw < 0) && (conv_type == ADC_SW)) 376 - return -ENOTSUPP; 377 - if ((gpadc->irq_hw < 0) && (conv_type == ADC_HW)) 378 - return -ENOTSUPP; 379 - 380 - mutex_lock(&gpadc->ab8500_gpadc_lock); 381 - /* Enable VTVout LDO this is required for GPADC */ 382 - pm_runtime_get_sync(gpadc->dev); 383 - 384 - /* Check if ADC is not busy, lock and proceed */ 385 - do { 386 - ret = abx500_get_register_interruptible(gpadc->dev, 387 - AB8500_GPADC, AB8500_GPADC_STAT_REG, &val); 388 - if (ret < 0) 389 - goto out; 390 - if (!(val & GPADC_BUSY)) 391 - break; 392 - msleep(20); 393 - } while (++looplimit < 10); 394 - if (looplimit >= 10 && (val & GPADC_BUSY)) { 395 - dev_err(gpadc->dev, "gpadc_conversion: GPADC busy"); 396 - ret = -EINVAL; 397 - goto out; 398 - } 399 - 400 - /* Enable GPADC */ 401 - val_reg1 |= EN_GPADC; 402 - 403 - /* Select the channel source and set average samples */ 404 - switch (avg_sample) { 405 - case SAMPLE_1: 406 - val = channel | AVG_1; 407 - break; 408 - case SAMPLE_4: 409 - val = channel | AVG_4; 410 - break; 411 - case SAMPLE_8: 412 - val = channel | AVG_8; 413 - break; 414 - default: 415 - val = channel | AVG_16; 416 - break; 417 - } 418 - 419 - if (conv_type == ADC_HW) { 420 - ret = abx500_set_register_interruptible(gpadc->dev, 421 - AB8500_GPADC, AB8500_GPADC_CTRL3_REG, val); 422 - val_reg1 |= EN_TRIG_EDGE; 423 - if (trig_edge) 424 - val_reg1 |= EN_FALLING; 425 - } else 426 - ret = abx500_set_register_interruptible(gpadc->dev, 427 - AB8500_GPADC, AB8500_GPADC_CTRL2_REG, val); 428 - if (ret < 0) { 429 - dev_err(gpadc->dev, 430 - "gpadc_conversion: set avg samples failed\n"); 431 - goto out; 432 - } 433 - 434 - /* 435 - * Enable ADC, buffering, select rising edge and enable ADC path 436 - * charging current sense if it needed, ABB 3.0 needs some special 437 - * treatment too. 438 - */ 439 - switch (channel) { 440 - case MAIN_CHARGER_C: 441 - case USB_CHARGER_C: 442 - val_reg1 |= EN_BUF | EN_ICHAR; 443 - break; 444 - case BTEMP_BALL: 445 - if (!is_ab8500_2p0_or_earlier(gpadc->parent)) { 446 - val_reg1 |= EN_BUF | BTEMP_PULL_UP; 447 - /* 448 - * Delay might be needed for ABB8500 cut 3.0, if not, 449 - * remove when hardware will be availible 450 - */ 451 - delay_min = 1000; /* Delay in micro seconds */ 452 - delay_max = 10000; /* large range optimises sleepmode */ 453 - break; 454 - } 455 - /* Intentional fallthrough */ 456 - default: 457 - val_reg1 |= EN_BUF; 458 - break; 459 - } 460 - 461 - /* Write configuration to register */ 462 - ret = abx500_set_register_interruptible(gpadc->dev, 463 - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, val_reg1); 464 - if (ret < 0) { 465 - dev_err(gpadc->dev, 466 - "gpadc_conversion: set Control register failed\n"); 467 - goto out; 468 - } 469 - 470 - if (delay_min != 0) 471 - usleep_range(delay_min, delay_max); 472 - 473 - if (conv_type == ADC_HW) { 474 - /* Set trigger delay timer */ 475 - ret = abx500_set_register_interruptible(gpadc->dev, 476 - AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG, trig_timer); 477 - if (ret < 0) { 478 - dev_err(gpadc->dev, 479 - "gpadc_conversion: trig timer failed\n"); 480 - goto out; 481 - } 482 - completion_timeout = 2 * HZ; 483 - data_low_addr = AB8500_GPADC_AUTODATAL_REG; 484 - data_high_addr = AB8500_GPADC_AUTODATAH_REG; 485 - } else { 486 - /* Start SW conversion */ 487 - ret = abx500_mask_and_set_register_interruptible(gpadc->dev, 488 - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, 489 - ADC_SW_CONV, ADC_SW_CONV); 490 - if (ret < 0) { 491 - dev_err(gpadc->dev, 492 - "gpadc_conversion: start s/w conv failed\n"); 493 - goto out; 494 - } 495 - completion_timeout = msecs_to_jiffies(CONVERSION_TIME); 496 - data_low_addr = AB8500_GPADC_MANDATAL_REG; 497 - data_high_addr = AB8500_GPADC_MANDATAH_REG; 498 - } 499 - 500 - /* wait for completion of conversion */ 501 - if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, 502 - completion_timeout)) { 503 - dev_err(gpadc->dev, 504 - "timeout didn't receive GPADC conv interrupt\n"); 505 - ret = -EINVAL; 506 - goto out; 507 - } 508 - 509 - /* Read the converted RAW data */ 510 - ret = abx500_get_register_interruptible(gpadc->dev, 511 - AB8500_GPADC, data_low_addr, &low_data); 512 - if (ret < 0) { 513 - dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n"); 514 - goto out; 515 - } 516 - 517 - ret = abx500_get_register_interruptible(gpadc->dev, 518 - AB8500_GPADC, data_high_addr, &high_data); 519 - if (ret < 0) { 520 - dev_err(gpadc->dev, "gpadc_conversion: read high data failed\n"); 521 - goto out; 522 - } 523 - 524 - /* Check if double convertion is required */ 525 - if ((channel == BAT_CTRL_AND_IBAT) || 526 - (channel == VBAT_MEAS_AND_IBAT) || 527 - (channel == VBAT_TRUE_MEAS_AND_IBAT) || 528 - (channel == BAT_TEMP_AND_IBAT)) { 529 - 530 - if (conv_type == ADC_HW) { 531 - /* not supported */ 532 - ret = -ENOTSUPP; 533 - dev_err(gpadc->dev, 534 - "gpadc_conversion: only SW double conversion supported\n"); 535 - goto out; 536 - } else { 537 - /* Read the converted RAW data 2 */ 538 - ret = abx500_get_register_interruptible(gpadc->dev, 539 - AB8500_GPADC, AB8540_GPADC_MANDATA2L_REG, 540 - &low_data2); 541 - if (ret < 0) { 542 - dev_err(gpadc->dev, 543 - "gpadc_conversion: read sw low data 2 failed\n"); 544 - goto out; 545 - } 546 - 547 - ret = abx500_get_register_interruptible(gpadc->dev, 548 - AB8500_GPADC, AB8540_GPADC_MANDATA2H_REG, 549 - &high_data2); 550 - if (ret < 0) { 551 - dev_err(gpadc->dev, 552 - "gpadc_conversion: read sw high data 2 failed\n"); 553 - goto out; 554 - } 555 - if (ibat != NULL) { 556 - *ibat = (high_data2 << 8) | low_data2; 557 - } else { 558 - dev_warn(gpadc->dev, 559 - "gpadc_conversion: ibat not stored\n"); 560 - } 561 - 562 - } 563 - } 564 - 565 - /* Disable GPADC */ 566 - ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, 567 - AB8500_GPADC_CTRL1_REG, DIS_GPADC); 568 - if (ret < 0) { 569 - dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n"); 570 - goto out; 571 - } 572 - 573 - /* Disable VTVout LDO this is required for GPADC */ 574 - pm_runtime_mark_last_busy(gpadc->dev); 575 - pm_runtime_put_autosuspend(gpadc->dev); 576 - 577 - mutex_unlock(&gpadc->ab8500_gpadc_lock); 578 - 579 - return (high_data << 8) | low_data; 580 - 581 - out: 582 - /* 583 - * It has shown to be needed to turn off the GPADC if an error occurs, 584 - * otherwise we might have problem when waiting for the busy bit in the 585 - * GPADC status register to go low. In V1.1 there wait_for_completion 586 - * seems to timeout when waiting for an interrupt.. Not seen in V2.0 587 - */ 588 - (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, 589 - AB8500_GPADC_CTRL1_REG, DIS_GPADC); 590 - pm_runtime_put(gpadc->dev); 591 - mutex_unlock(&gpadc->ab8500_gpadc_lock); 592 - dev_err(gpadc->dev, 593 - "gpadc_conversion: Failed to AD convert channel %d\n", channel); 594 - return ret; 595 - } 596 - EXPORT_SYMBOL(ab8500_gpadc_read_raw); 597 - 598 - /** 599 - * ab8500_bm_gpadcconvend_handler() - isr for gpadc conversion completion 600 - * @irq: irq number 601 - * @data: pointer to the data passed during request irq 602 - * 603 - * This is a interrupt service routine for gpadc conversion completion. 604 - * Notifies the gpadc completion is completed and the converted raw value 605 - * can be read from the registers. 606 - * Returns IRQ status(IRQ_HANDLED) 607 - */ 608 - static irqreturn_t ab8500_bm_gpadcconvend_handler(int irq, void *_gpadc) 609 - { 610 - struct ab8500_gpadc *gpadc = _gpadc; 611 - 612 - complete(&gpadc->ab8500_gpadc_complete); 613 - 614 - return IRQ_HANDLED; 615 - } 616 - 617 - static int otp_cal_regs[] = { 618 - AB8500_GPADC_CAL_1, 619 - AB8500_GPADC_CAL_2, 620 - AB8500_GPADC_CAL_3, 621 - AB8500_GPADC_CAL_4, 622 - AB8500_GPADC_CAL_5, 623 - AB8500_GPADC_CAL_6, 624 - AB8500_GPADC_CAL_7, 625 - }; 626 - 627 - static int otp4_cal_regs[] = { 628 - AB8540_GPADC_OTP4_REG_7, 629 - AB8540_GPADC_OTP4_REG_6, 630 - AB8540_GPADC_OTP4_REG_5, 631 - }; 632 - 633 - static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc) 634 - { 635 - int i; 636 - int ret[ARRAY_SIZE(otp_cal_regs)]; 637 - u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)]; 638 - int ret_otp4[ARRAY_SIZE(otp4_cal_regs)]; 639 - u8 gpadc_otp4[ARRAY_SIZE(otp4_cal_regs)]; 640 - int vmain_high, vmain_low; 641 - int btemp_high, btemp_low; 642 - int vbat_high, vbat_low; 643 - int ibat_high, ibat_low; 644 - s64 V_gain, V_offset, V2A_gain, V2A_offset; 645 - struct ab8500 *ab8500; 646 - 647 - ab8500 = gpadc->parent; 648 - 649 - /* First we read all OTP registers and store the error code */ 650 - for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) { 651 - ret[i] = abx500_get_register_interruptible(gpadc->dev, 652 - AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]); 653 - if (ret[i] < 0) 654 - dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n", 655 - __func__, otp_cal_regs[i]); 656 - } 657 - 658 - /* 659 - * The ADC calibration data is stored in OTP registers. 660 - * The layout of the calibration data is outlined below and a more 661 - * detailed description can be found in UM0836 662 - * 663 - * vm_h/l = vmain_high/low 664 - * bt_h/l = btemp_high/low 665 - * vb_h/l = vbat_high/low 666 - * 667 - * Data bits 8500/9540: 668 - * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 669 - * |.......|.......|.......|.......|.......|.......|.......|....... 670 - * | | vm_h9 | vm_h8 671 - * |.......|.......|.......|.......|.......|.......|.......|....... 672 - * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2 673 - * |.......|.......|.......|.......|.......|.......|.......|....... 674 - * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9 675 - * |.......|.......|.......|.......|.......|.......|.......|....... 676 - * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1 677 - * |.......|.......|.......|.......|.......|.......|.......|....... 678 - * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8 679 - * |.......|.......|.......|.......|.......|.......|.......|....... 680 - * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0 681 - * |.......|.......|.......|.......|.......|.......|.......|....... 682 - * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 | 683 - * |.......|.......|.......|.......|.......|.......|.......|....... 684 - * 685 - * Data bits 8540: 686 - * OTP2 687 - * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 688 - * |.......|.......|.......|.......|.......|.......|.......|....... 689 - * | 690 - * |.......|.......|.......|.......|.......|.......|.......|....... 691 - * | vm_h9 | vm_h8 | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2 692 - * |.......|.......|.......|.......|.......|.......|.......|....... 693 - * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9 694 - * |.......|.......|.......|.......|.......|.......|.......|....... 695 - * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1 696 - * |.......|.......|.......|.......|.......|.......|.......|....... 697 - * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8 698 - * |.......|.......|.......|.......|.......|.......|.......|....... 699 - * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0 700 - * |.......|.......|.......|.......|.......|.......|.......|....... 701 - * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 | 702 - * |.......|.......|.......|.......|.......|.......|.......|....... 703 - * 704 - * Data bits 8540: 705 - * OTP4 706 - * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 707 - * |.......|.......|.......|.......|.......|.......|.......|....... 708 - * | | ib_h9 | ib_h8 | ib_h7 709 - * |.......|.......|.......|.......|.......|.......|.......|....... 710 - * | ib_h6 | ib_h5 | ib_h4 | ib_h3 | ib_h2 | ib_h1 | ib_h0 | ib_l5 711 - * |.......|.......|.......|.......|.......|.......|.......|....... 712 - * | ib_l4 | ib_l3 | ib_l2 | ib_l1 | ib_l0 | 713 - * 714 - * 715 - * Ideal output ADC codes corresponding to injected input voltages 716 - * during manufacturing is: 717 - * 718 - * vmain_high: Vin = 19500mV / ADC ideal code = 997 719 - * vmain_low: Vin = 315mV / ADC ideal code = 16 720 - * btemp_high: Vin = 1300mV / ADC ideal code = 985 721 - * btemp_low: Vin = 21mV / ADC ideal code = 16 722 - * vbat_high: Vin = 4700mV / ADC ideal code = 982 723 - * vbat_low: Vin = 2380mV / ADC ideal code = 33 724 - */ 725 - 726 - if (is_ab8540(ab8500)) { 727 - /* Calculate gain and offset for VMAIN if all reads succeeded*/ 728 - if (!(ret[1] < 0 || ret[2] < 0)) { 729 - vmain_high = (((gpadc_cal[1] & 0xFF) << 2) | 730 - ((gpadc_cal[2] & 0xC0) >> 6)); 731 - vmain_low = ((gpadc_cal[2] & 0x3E) >> 1); 732 - 733 - gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_hi = 734 - (u16)vmain_high; 735 - gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_lo = 736 - (u16)vmain_low; 737 - 738 - gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE * 739 - (19500 - 315) / (vmain_high - vmain_low); 740 - gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 741 - 19500 - (CALIB_SCALE * (19500 - 315) / 742 - (vmain_high - vmain_low)) * vmain_high; 743 - } else { 744 - gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0; 745 - } 746 - 747 - /* Read IBAT calibration Data */ 748 - for (i = 0; i < ARRAY_SIZE(otp4_cal_regs); i++) { 749 - ret_otp4[i] = abx500_get_register_interruptible( 750 - gpadc->dev, AB8500_OTP_EMUL, 751 - otp4_cal_regs[i], &gpadc_otp4[i]); 752 - if (ret_otp4[i] < 0) 753 - dev_err(gpadc->dev, 754 - "%s: read otp4 reg 0x%02x failed\n", 755 - __func__, otp4_cal_regs[i]); 756 - } 757 - 758 - /* Calculate gain and offset for IBAT if all reads succeeded */ 759 - if (!(ret_otp4[0] < 0 || ret_otp4[1] < 0 || ret_otp4[2] < 0)) { 760 - ibat_high = (((gpadc_otp4[0] & 0x07) << 7) | 761 - ((gpadc_otp4[1] & 0xFE) >> 1)); 762 - ibat_low = (((gpadc_otp4[1] & 0x01) << 5) | 763 - ((gpadc_otp4[2] & 0xF8) >> 3)); 764 - 765 - gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_hi = 766 - (u16)ibat_high; 767 - gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_lo = 768 - (u16)ibat_low; 769 - 770 - V_gain = ((IBAT_VDROP_H - IBAT_VDROP_L) 771 - << CALIB_SHIFT_IBAT) / (ibat_high - ibat_low); 772 - 773 - V_offset = (IBAT_VDROP_H << CALIB_SHIFT_IBAT) - 774 - (((IBAT_VDROP_H - IBAT_VDROP_L) << 775 - CALIB_SHIFT_IBAT) / (ibat_high - ibat_low)) 776 - * ibat_high; 777 - /* 778 - * Result obtained is in mV (at a scale factor), 779 - * we need to calculate gain and offset to get mA 780 - */ 781 - V2A_gain = (ADC_CH_IBAT_MAX - ADC_CH_IBAT_MIN)/ 782 - (ADC_CH_IBAT_MAX_V - ADC_CH_IBAT_MIN_V); 783 - V2A_offset = ((ADC_CH_IBAT_MAX_V * ADC_CH_IBAT_MIN - 784 - ADC_CH_IBAT_MAX * ADC_CH_IBAT_MIN_V) 785 - << CALIB_SHIFT_IBAT) 786 - / (ADC_CH_IBAT_MAX_V - ADC_CH_IBAT_MIN_V); 787 - 788 - gpadc->cal_data[ADC_INPUT_IBAT].gain = 789 - V_gain * V2A_gain; 790 - gpadc->cal_data[ADC_INPUT_IBAT].offset = 791 - V_offset * V2A_gain + V2A_offset; 792 - } else { 793 - gpadc->cal_data[ADC_INPUT_IBAT].gain = 0; 794 - } 795 - 796 - dev_dbg(gpadc->dev, "IBAT gain %llu offset %llu\n", 797 - gpadc->cal_data[ADC_INPUT_IBAT].gain, 798 - gpadc->cal_data[ADC_INPUT_IBAT].offset); 799 - } else { 800 - /* Calculate gain and offset for VMAIN if all reads succeeded */ 801 - if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) { 802 - vmain_high = (((gpadc_cal[0] & 0x03) << 8) | 803 - ((gpadc_cal[1] & 0x3F) << 2) | 804 - ((gpadc_cal[2] & 0xC0) >> 6)); 805 - vmain_low = ((gpadc_cal[2] & 0x3E) >> 1); 806 - 807 - gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_hi = 808 - (u16)vmain_high; 809 - gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_lo = 810 - (u16)vmain_low; 811 - 812 - gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE * 813 - (19500 - 315) / (vmain_high - vmain_low); 814 - 815 - gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 816 - 19500 - (CALIB_SCALE * (19500 - 315) / 817 - (vmain_high - vmain_low)) * vmain_high; 818 - } else { 819 - gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0; 820 - } 821 - } 822 - 823 - /* Calculate gain and offset for BTEMP if all reads succeeded */ 824 - if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) { 825 - btemp_high = (((gpadc_cal[2] & 0x01) << 9) | 826 - (gpadc_cal[3] << 1) | ((gpadc_cal[4] & 0x80) >> 7)); 827 - btemp_low = ((gpadc_cal[4] & 0x7C) >> 2); 828 - 829 - gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_hi = (u16)btemp_high; 830 - gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_lo = (u16)btemp_low; 831 - 832 - gpadc->cal_data[ADC_INPUT_BTEMP].gain = 833 - CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low); 834 - gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 - 835 - (CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low)) 836 - * btemp_high; 837 - } else { 838 - gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0; 839 - } 840 - 841 - /* Calculate gain and offset for VBAT if all reads succeeded */ 842 - if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) { 843 - vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]); 844 - vbat_low = ((gpadc_cal[6] & 0xFC) >> 2); 845 - 846 - gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_hi = (u16)vbat_high; 847 - gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_lo = (u16)vbat_low; 848 - 849 - gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE * 850 - (4700 - 2380) / (vbat_high - vbat_low); 851 - gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 - 852 - (CALIB_SCALE * (4700 - 2380) / 853 - (vbat_high - vbat_low)) * vbat_high; 854 - } else { 855 - gpadc->cal_data[ADC_INPUT_VBAT].gain = 0; 856 - } 857 - 858 - dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n", 859 - gpadc->cal_data[ADC_INPUT_VMAIN].gain, 860 - gpadc->cal_data[ADC_INPUT_VMAIN].offset); 861 - 862 - dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n", 863 - gpadc->cal_data[ADC_INPUT_BTEMP].gain, 864 - gpadc->cal_data[ADC_INPUT_BTEMP].offset); 865 - 866 - dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n", 867 - gpadc->cal_data[ADC_INPUT_VBAT].gain, 868 - gpadc->cal_data[ADC_INPUT_VBAT].offset); 869 - } 870 - 871 - #ifdef CONFIG_PM 872 - static int ab8500_gpadc_runtime_suspend(struct device *dev) 873 - { 874 - struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); 875 - 876 - regulator_disable(gpadc->regu); 877 - return 0; 878 - } 879 - 880 - static int ab8500_gpadc_runtime_resume(struct device *dev) 881 - { 882 - struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); 883 - int ret; 884 - 885 - ret = regulator_enable(gpadc->regu); 886 - if (ret) 887 - dev_err(dev, "Failed to enable vtvout LDO: %d\n", ret); 888 - return ret; 889 - } 890 - #endif 891 - 892 - #ifdef CONFIG_PM_SLEEP 893 - static int ab8500_gpadc_suspend(struct device *dev) 894 - { 895 - struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); 896 - 897 - mutex_lock(&gpadc->ab8500_gpadc_lock); 898 - 899 - pm_runtime_get_sync(dev); 900 - 901 - regulator_disable(gpadc->regu); 902 - return 0; 903 - } 904 - 905 - static int ab8500_gpadc_resume(struct device *dev) 906 - { 907 - struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); 908 - int ret; 909 - 910 - ret = regulator_enable(gpadc->regu); 911 - if (ret) 912 - dev_err(dev, "Failed to enable vtvout LDO: %d\n", ret); 913 - 914 - pm_runtime_mark_last_busy(gpadc->dev); 915 - pm_runtime_put_autosuspend(gpadc->dev); 916 - 917 - mutex_unlock(&gpadc->ab8500_gpadc_lock); 918 - return ret; 919 - } 920 - #endif 921 - 922 - static int ab8500_gpadc_probe(struct platform_device *pdev) 923 - { 924 - int ret = 0; 925 - struct ab8500_gpadc *gpadc; 926 - 927 - gpadc = devm_kzalloc(&pdev->dev, 928 - sizeof(struct ab8500_gpadc), GFP_KERNEL); 929 - if (!gpadc) 930 - return -ENOMEM; 931 - 932 - gpadc->irq_sw = platform_get_irq_byname(pdev, "SW_CONV_END"); 933 - if (gpadc->irq_sw < 0) 934 - dev_err(gpadc->dev, "failed to get platform sw_conv_end irq\n"); 935 - 936 - gpadc->irq_hw = platform_get_irq_byname(pdev, "HW_CONV_END"); 937 - if (gpadc->irq_hw < 0) 938 - dev_err(gpadc->dev, "failed to get platform hw_conv_end irq\n"); 939 - 940 - gpadc->dev = &pdev->dev; 941 - gpadc->parent = dev_get_drvdata(pdev->dev.parent); 942 - mutex_init(&gpadc->ab8500_gpadc_lock); 943 - 944 - /* Initialize completion used to notify completion of conversion */ 945 - init_completion(&gpadc->ab8500_gpadc_complete); 946 - 947 - /* Register interrupts */ 948 - if (gpadc->irq_sw >= 0) { 949 - ret = request_threaded_irq(gpadc->irq_sw, NULL, 950 - ab8500_bm_gpadcconvend_handler, 951 - IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT, 952 - "ab8500-gpadc-sw", 953 - gpadc); 954 - if (ret < 0) { 955 - dev_err(gpadc->dev, 956 - "Failed to register interrupt irq: %d\n", 957 - gpadc->irq_sw); 958 - goto fail; 959 - } 960 - } 961 - 962 - if (gpadc->irq_hw >= 0) { 963 - ret = request_threaded_irq(gpadc->irq_hw, NULL, 964 - ab8500_bm_gpadcconvend_handler, 965 - IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT, 966 - "ab8500-gpadc-hw", 967 - gpadc); 968 - if (ret < 0) { 969 - dev_err(gpadc->dev, 970 - "Failed to register interrupt irq: %d\n", 971 - gpadc->irq_hw); 972 - goto fail_irq; 973 - } 974 - } 975 - 976 - /* VTVout LDO used to power up ab8500-GPADC */ 977 - gpadc->regu = devm_regulator_get(&pdev->dev, "vddadc"); 978 - if (IS_ERR(gpadc->regu)) { 979 - ret = PTR_ERR(gpadc->regu); 980 - dev_err(gpadc->dev, "failed to get vtvout LDO\n"); 981 - goto fail_irq; 982 - } 983 - 984 - platform_set_drvdata(pdev, gpadc); 985 - 986 - ret = regulator_enable(gpadc->regu); 987 - if (ret) { 988 - dev_err(gpadc->dev, "Failed to enable vtvout LDO: %d\n", ret); 989 - goto fail_enable; 990 - } 991 - 992 - pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY); 993 - pm_runtime_use_autosuspend(gpadc->dev); 994 - pm_runtime_set_active(gpadc->dev); 995 - pm_runtime_enable(gpadc->dev); 996 - 997 - ab8500_gpadc_read_calibration_data(gpadc); 998 - list_add_tail(&gpadc->node, &ab8500_gpadc_list); 999 - dev_dbg(gpadc->dev, "probe success\n"); 1000 - 1001 - return 0; 1002 - 1003 - fail_enable: 1004 - fail_irq: 1005 - free_irq(gpadc->irq_sw, gpadc); 1006 - free_irq(gpadc->irq_hw, gpadc); 1007 - fail: 1008 - return ret; 1009 - } 1010 - 1011 - static int ab8500_gpadc_remove(struct platform_device *pdev) 1012 - { 1013 - struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev); 1014 - 1015 - /* remove this gpadc entry from the list */ 1016 - list_del(&gpadc->node); 1017 - /* remove interrupt - completion of Sw ADC conversion */ 1018 - if (gpadc->irq_sw >= 0) 1019 - free_irq(gpadc->irq_sw, gpadc); 1020 - if (gpadc->irq_hw >= 0) 1021 - free_irq(gpadc->irq_hw, gpadc); 1022 - 1023 - pm_runtime_get_sync(gpadc->dev); 1024 - pm_runtime_disable(gpadc->dev); 1025 - 1026 - regulator_disable(gpadc->regu); 1027 - 1028 - pm_runtime_set_suspended(gpadc->dev); 1029 - 1030 - pm_runtime_put_noidle(gpadc->dev); 1031 - 1032 - return 0; 1033 - } 1034 - 1035 - static const struct dev_pm_ops ab8500_gpadc_pm_ops = { 1036 - SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend, 1037 - ab8500_gpadc_runtime_resume, 1038 - NULL) 1039 - SET_SYSTEM_SLEEP_PM_OPS(ab8500_gpadc_suspend, 1040 - ab8500_gpadc_resume) 1041 - 1042 - }; 1043 - 1044 - static struct platform_driver ab8500_gpadc_driver = { 1045 - .probe = ab8500_gpadc_probe, 1046 - .remove = ab8500_gpadc_remove, 1047 - .driver = { 1048 - .name = "ab8500-gpadc", 1049 - .pm = &ab8500_gpadc_pm_ops, 1050 - }, 1051 - }; 1052 - 1053 - static int __init ab8500_gpadc_init(void) 1054 - { 1055 - return platform_driver_register(&ab8500_gpadc_driver); 1056 - } 1057 - subsys_initcall_sync(ab8500_gpadc_init); 1058 - 1059 - /** 1060 - * ab8540_gpadc_get_otp() - returns OTP values 1061 - * 1062 - */ 1063 - void ab8540_gpadc_get_otp(struct ab8500_gpadc *gpadc, 1064 - u16 *vmain_l, u16 *vmain_h, u16 *btemp_l, u16 *btemp_h, 1065 - u16 *vbat_l, u16 *vbat_h, u16 *ibat_l, u16 *ibat_h) 1066 - { 1067 - *vmain_l = gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_lo; 1068 - *vmain_h = gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_hi; 1069 - *btemp_l = gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_lo; 1070 - *btemp_h = gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_hi; 1071 - *vbat_l = gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_lo; 1072 - *vbat_h = gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_hi; 1073 - *ibat_l = gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_lo; 1074 - *ibat_h = gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_hi; 1075 - }
-75
include/linux/mfd/abx500/ab8500-gpadc.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * Copyright (C) 2010 ST-Ericsson SA 4 - * 5 - * Author: Arun R Murthy <arun.murthy@stericsson.com> 6 - * Author: Daniel Willerud <daniel.willerud@stericsson.com> 7 - * Author: M'boumba Cedric Madianga <cedric.madianga@stericsson.com> 8 - */ 9 - 10 - #ifndef _AB8500_GPADC_H 11 - #define _AB8500_GPADC_H 12 - 13 - /* GPADC source: From datasheet(ADCSwSel[4:0] in GPADCCtrl2 14 - * and ADCHwSel[4:0] in GPADCCtrl3 ) */ 15 - #define BAT_CTRL 0x01 16 - #define BTEMP_BALL 0x02 17 - #define MAIN_CHARGER_V 0x03 18 - #define ACC_DETECT1 0x04 19 - #define ACC_DETECT2 0x05 20 - #define ADC_AUX1 0x06 21 - #define ADC_AUX2 0x07 22 - #define MAIN_BAT_V 0x08 23 - #define VBUS_V 0x09 24 - #define MAIN_CHARGER_C 0x0A 25 - #define USB_CHARGER_C 0x0B 26 - #define BK_BAT_V 0x0C 27 - #define DIE_TEMP 0x0D 28 - #define USB_ID 0x0E 29 - #define XTAL_TEMP 0x12 30 - #define VBAT_TRUE_MEAS 0x13 31 - #define BAT_CTRL_AND_IBAT 0x1C 32 - #define VBAT_MEAS_AND_IBAT 0x1D 33 - #define VBAT_TRUE_MEAS_AND_IBAT 0x1E 34 - #define BAT_TEMP_AND_IBAT 0x1F 35 - 36 - /* Virtual channel used only for ibat convertion to ampere 37 - * Battery current conversion (ibat) cannot be requested as a single conversion 38 - * but it is always in combination with other input requests 39 - */ 40 - #define IBAT_VIRTUAL_CHANNEL 0xFF 41 - 42 - #define SAMPLE_1 1 43 - #define SAMPLE_4 4 44 - #define SAMPLE_8 8 45 - #define SAMPLE_16 16 46 - #define RISING_EDGE 0 47 - #define FALLING_EDGE 1 48 - 49 - /* Arbitrary ADC conversion type constants */ 50 - #define ADC_SW 0 51 - #define ADC_HW 1 52 - 53 - struct ab8500_gpadc; 54 - 55 - struct ab8500_gpadc *ab8500_gpadc_get(char *name); 56 - int ab8500_gpadc_sw_hw_convert(struct ab8500_gpadc *gpadc, u8 channel, 57 - u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type); 58 - static inline int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 channel) 59 - { 60 - return ab8500_gpadc_sw_hw_convert(gpadc, channel, 61 - SAMPLE_16, 0, 0, ADC_SW); 62 - } 63 - 64 - int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel, 65 - u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type); 66 - int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, 67 - u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type, 68 - int *ibat); 69 - int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, 70 - u8 channel, int ad_value); 71 - void ab8540_gpadc_get_otp(struct ab8500_gpadc *gpadc, 72 - u16 *vmain_l, u16 *vmain_h, u16 *btemp_l, u16 *btemp_h, 73 - u16 *vbat_l, u16 *vbat_h, u16 *ibat_l, u16 *ibat_h); 74 - 75 - #endif /* _AB8500_GPADC_H */